MapActivity の開始時に現在のデバイスの Location をロードする必要があります (別のアクティビティから起動された Intent で開始されます)。
問題は、Web を見回すと、Android 開発者セクションで、LocationListener メソッド onLocationChange(location) を使用する必要があることがわかりますが、ユーザーが場所を変更したときではなく、MapActivity が開始したときにユーザーの場所を取得する必要があることです。私はいくつかの方法を試しましたが、解決策が見つかりません。
public class MappaActivity extends MapActivity {
MapView miaMapView;
MapController mc;
LocationManager lm;
GeoPoint actualP;
LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_mappa_activity);
miaMapView=(MapView)findViewById(R.id.miaMapView);
mc=miaMapView.getController();
miaMapView.setBuiltInZoomControls(true);
miaMapView.setSatellite(true);
miaMapView.invalidate();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener=new MyLocationListener();
lm.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener);
// This is MyLocationListener inner class:
private class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if(location!=null){
actualP = new
GeoPoint(
(int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
mc.animateTo(actualP);
mc.setZoom(10);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
actualP は null のままで、ユーザーの場所が変更されるまで onLocationChanged メソッドは起動されません。
locationManager.getLastKnowLocation(location.GPS_PROVIDER) も試しましたが、null が返されます。
私はいくつかの助けを見つけることを願っています, ありがとう!