Enlarge display of user's current location

I am using Google Maps for Android v2. I would like to display the current location of the user and increase it. Here is the code in the FragmentActivity :

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_activity); mMap = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.mapFragment_mapActivity)).getMap(); if (mMap == null) { Toast.makeText(this, R.string.mapCreationProblem, Toast.LENGTH_LONG) .show(); finish(); return; } mMap.setMyLocationEnabled(true); Location currentLocation = mMap.getMyLocation(); if(currentLocation!=null){ LatLng currentCoordinates = new LatLng( currentLocation.getLatitude(), currentLocation.getLongitude()); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentCoordinates, 10)); } } 

The map works fine, as well as the blue dot at the current location. But it seems that currentLocation always zero. Therefore, I cannot increase the current location.

Who knows why, please?

+1
android android-maps-v2 android-maps
Jan 24 '13 at 13:08
source share
2 answers

Here is an implementation of getMyLocation () with additional protection for finding the location of individuals. I just have this in the activity that controls the map fragment.

 private Location getMyLocation() { // Get location from GPS if it available LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); // Location wasn't found, check the next most accurate place for the current location if (myLocation == null) { Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); // Finds a provider that matches the criteria String provider = lm.getBestProvider(criteria, true); // Use the provider to get the last known location myLocation = lm.getLastKnownLocation(provider); } return myLocation; } 

Thanks DMan

+8
Jan 24 '13 at 9:21
source share

Try using the LocationManager:

 LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
+1
Jan 24 '13 at 13:19
source share



All Articles