我正在开发一个Android应用程序,它在地图中显示用户的当前位置。
几小时后,我发现了错误,locationManager没有实现位置。
我把代码放在locationManager:
mapa = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapaTapas)).getMap();
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager)getSystemService (serviceString);
String proovedor1 = LocationManager.GPS_PROVIDER;
locationManager.getLastKnownLocation(proovedor1);
String proovedor2 = LocationManager.NETWORK_PROVIDER;
locationManager.getLastKnownLocation(proovedor2);
locationManager.requestLocationUpdates(proovedor1, 5000, 5, new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1,Bundle arg2) { }
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onLocationChanged(Location Location)
{
location=Location;
}
});
locationManager.requestLocationUpdates(proovedor2, 5000, 5, new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1,Bundle arg2) { }
@Override
public void onProviderEnabled(String arg0) {}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onLocationChanged(Location Location)
{
location=Location;
}
});
location = locationManager.getLastKnownLocation(towers);
if(location != null){
glat = location.getLatitude();
glon = location.getLongitude();
Log.i("location", "latitud: "+glat);
Log.i("location", "latitud: "+glon);
}
cp = new CameraPosition.Builder()
.target(new LatLng(glat, glon))
.zoom(15)
.build();
mapa.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
我需要在glat和glon中保存位置的当前值,如果它是posibble实现一些方法来自动刷新它
答案 0 :(得分:0)
现在,LocationManager
界面为introduced,无法从OnMyLocationChangeListener
手动请求位置更新。
您只需要启用我的位置图层(map.setMyLocationEnabled(true)
),设置监听器(map.setOnMyLocationChangeListener(this)
)并使用当前位置(例如动画相机)执行任何操作,然后传回给您相应的回调(通过实现该接口提供)。