我正在研究一個項目,該項目具有顯示(動畫)當前用戶位置和一個硬編碼位置(給定經度和緯度)的功能。到目前爲止,我有硬編碼的位置工作,但我無法弄清楚如何獲得用戶位置。我試過位置偵聽器,但是我的應用程序在運行後不斷崩潰。有人對此有任何建議嗎?非常感激。下面的代碼:Animate to User Location(Android)
public class LocationsActivity extends MapActivity implements LocationListener {
MapView mapView;
MapController mc;
GeoPoint p;
LocationListener locListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"52.67596","-8.64910"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
OverlayItem overlayitem = new OverlayItem(p, "Limerick Institute of
Technology", "Moylish");
mc.animateTo(p);
mc.setZoom(17);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,
this);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
讓您的解決方案from here..http://android-er.blogspot.in/2009/11/mapview-to-center-on-current-location.html – Bhavin 2012-04-27 13:17:12
please a dd崩潰日誌.. – Ronnie 2012-04-27 13:23:17
@ userSeven7s它就像IllegalArgumentException:listener == null。我認爲這與事實有關,如果你更新當前位置,它可能返回一個空值。 – 2012-04-27 13:29:25