Android Google Maps V2 User Location

10,774

Solution 1

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.

Solution 2

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));
        }
}
Share:
10,774
Mark Molina
Author by

Mark Molina

Co-Owner of De Nederlandse Wateren http://www.denederlandsewateren.nl/ Owner of CleverCode http://www.clevercode.nl/ iOS and Android developer LinkedIn: http://nl.linkedin.com/pub/mark-molina/54/45/186/

Updated on June 04, 2022

Comments

  • Mark Molina
    Mark Molina almost 2 years

    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"/>