0

i created an Android activity to show Google Maps. I would center the map to the current user position but i cannot figure out how to do it.

Here is my current code:

Location location = getMyLocation();
LatLng userPos = new LatLng(location.getLatitude(), location.getLongitude());

map.animateCamera(CameraUpdateFactory.newLatLngZoom(userPos, zoomLevel));

CameraPosition cameraPosition = new CameraPosition.Builder()
                                    .target(userPos)
                                    .zoom(17)
                                    .bearing(90)
                                    .tilt(40)
                                    .build();

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

The source of this code is this answer (where i copied getMyLocation() from) and this solution, but they don't work. In both cases it doesn't work at all: the center is not set and weirdly neither the zoom level is set.

If i use just this code (without current user position):

map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(42.564241, 12.22759), zoomLevel));

it works with no problem, even the zoom level is set.

What could be the problem?

Community
  • 1
  • 1
smartmouse
  • 13,912
  • 34
  • 100
  • 166

1 Answers1

1

make sure you have these permissions: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> Then make some activity and register a LocationListener

package com.example.location;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class LocationActivity extends SherlockFragmentActivity implements 
LocationListener     {       
private GoogleMap map;    
private LocationManager locationManager;
private static final long MIN_TIME = 400;
private static final float MIN_DISTANCE = 1000;

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
map = ((SupportMapFragment)     
getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

locationManager = (LocationManager)   
getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,   
MIN_TIME, MIN_DISTANCE, this); //You can also use   
LocationManager.GPS_PROVIDER and LocationManager.PASSIVE_PROVIDER        
 }
 @Override
public void onLocationChanged(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
locationManager.removeUpdates(this);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onProviderDisabled(String provider) { }
}'
Mayuri
  • 489
  • 3
  • 11