您需要向GoogleMap添加一个LocationSource,并响应onLocationChanged事件。这里有一个简单的类,它会请求用户的位置,然后等待直到用户的位置可用,并将地图动画定位到他们的位置中心。
public class MyLocationMapFragmentActivity extends FragmentActivity implements LocationListener, LocationSource
{
private GoogleMap mMap;
private OnLocationChangedListener mListener;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_map);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(locationManager != null)
{
boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gpsIsEnabled)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);
}
else if(networkIsEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L, 10F, this);
}
else
{
}
}
else
{
}
setUpMapIfNeeded();
}
@Override
public void onPause()
{
if(locationManager != null)
{
locationManager.removeUpdates(this);
}
super.onPause();
}
@Override
public void onResume()
{
super.onResume();
setUpMapIfNeeded();
if(locationManager != null)
{
mMap.setMyLocationEnabled(true);
}
}
private void setUpMapIfNeeded() {
if (mMap == null)
{
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap();
if (mMap != null)
{
setUpMap();
}
mMap.setLocationSource(this);
}
}
private void setUpMap()
{
mMap.setMyLocationEnabled(true);
}
@Override
public void activate(OnLocationChangedListener listener)
{
mListener = listener;
}
@Override
public void deactivate()
{
mListener = null;
}
@Override
public void onLocationChanged(Location location)
{
if( mListener != null )
{
mListener.onLocationChanged( location );
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show();
}
}
这应该跟随用户并在其位置发生变化时保持地图居中 - 如果您只想在用户“离开屏幕”时将地图居中于用户,则可以检查用户的位置是否在地图的可见边界内。
@Override
public void onLocationChanged(Location location)
{
if( mListener != null )
{
mListener.onLocationChanged( location );
LatLngBounds bounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
if(!bounds.contains(new LatLng(location.getLatitude(), location.getLongitude())))
{
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}
}
}
以下是关于该主题的简短介绍:
Google Maps Android API V2 MyLocation LocationSource和事件处理
更新
我修改了获取用户位置的方式(请参见onCreate中的locationManager代码)。我发现使用“getBestProvider”方法不可靠,并且在几台设备上根本无法工作。我有几个用户抱怨他们的设备永远无法找到自己的位置,无论我放松标准多少。手动选择GPS或网络似乎是普遍适用的。