简体   繁体   中英

Android Google Maps V2 User Location

The blue dot/arrow isnt showing on my map. Everything else works fine. Am i missing some permissions?

Included the Java Class, Manifest and layout XML.

private void setUpMap(int satelliteMode, LatLng startPoint, float zoomLevel) {

        mapView.getUiSettings().setZoomControlsEnabled(false);
        //mapView.getUiSettings().setMyLocationButtonEnabled(false);
        mapView.setMapType(satelliteMode);
        mapView.setMyLocationEnabled(true);
        mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint, zoomLevel));
    }

xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

manifest:

<permission
         android:name="com.example.project.MAPS_RECEIVE"
         android:protectionLevel="signature"/>
    <uses-permission  android:name="com.example.project.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

        <meta-data
           android:name="com.google.android.maps.v2.API_KEY"
           android:value="API_KEY"/>

You'd see that clicking the 'my location' button creates the blue dot at your current location. Using the location manager to track location updates and moving the camera accordingly allows you to track the user too, without clicking the button. See a snippet below.

LocationListener ll = new LocationListener() {

        @Override
        public void onLocationChanged(Location arg0) {
            Toast.makeText(getBaseContext(), "Moved to "+arg0.toString(), Toast.LENGTH_LONG).show();
            CameraPosition cp = new CameraPosition.Builder()
            .target(new LatLng(arg0.getLatitude(),arg0.getLongitude()))
            .zoom(12)
            .build();     
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
        }
}

For some reason it just worked. Went away for lunch and left the app on. When i returned the blue arrow was drawn on the map. Not on the right location though but it was on the map. So the posted code of mine works. It only needs an update from the location manager.

FWIW, i have the exact same problem as the original post on a Samsung Galaxy Tab 2. However, i do NOT have the issue on an Archos 101G9.

If I reboot, load my app with Google Maps v2 and click the 'go to my location' button in the top right, the Archos goes directly to my current location but the SGT2 does nothing.

If I manually get my current location using location manager and animate the map to it then it navigates. However clicking the 'go to my location' button still does nothing.

Getting a location fix using GPS seems to fix the issue.

EDIT: On the SGT2 the blue location dot appears only with a GPS fix, however the Archos displays the blue dot with a mobile data/wifi fix.

Hi in you FragmentActivity add implements LocationListener

private LocationManager locationManager;

In onCreate(Bundle savedInstanceState) .... add

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);       locationManager.requestLocationUpdates(locationManager.getBestProvider(locationCriteria, true), 1L, 2F, this);

and implemented methos to LocationListener ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM