我在獲取當前用戶位置時遇到問題。代碼起初可以工作,但過了一段時間它就停止顯示當前位置。我嘗試了幾種方法來解決這個問題,但結果相同。Android Google地圖獲取當前位置不起作用
這裏是我的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapscreen);
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
String provider = getProvider();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//getLastKnownPoint();
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
drawCurrPositionOverlay();
}
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
GeoPoint currentPoint = getCurrentPoint(location);
animateToCurrentPoint(currentPoint);
currentLocation = location;
currentGeoPoint = currentPoint;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
};
public GeoPoint getCurrentPoint (Location location){
GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
return currentPoint;
}
public GeoPoint getLastKnownPoint(){
GeoPoint lastKnownPoint = null;
Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
if(lastKnownLocation != null){
lastKnownPoint = getCurrentPoint(lastKnownLocation);
}
return lastKnownPoint;
}
public void animateToCurrentPoint(GeoPoint currentPoint){
mapController.animateTo(currentPoint);
mapController.setCenter(currentPoint);
mapController.setZoom(15);
}
public void drawCurrPositionOverlay(){
List<Overlay> overlays = mapView.getOverlays();
CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
overlays.remove(currPos);
Drawable marker = getResources().getDrawable(R.drawable.me);
currPos = new MapOverlay(marker,mapView);
GeoPoint drawMyPoint = null;
if(currentGeoPoint==null){
drawMyPoint = getLastKnownPoint();
}
else {
drawMyPoint = currentGeoPoint;
}
OverlayItem overlayitem = new OverlayItem(drawMyPoint, "I'm here ", "wee");
currPos.addOverlay(overlayitem);
overlays.add(currPos);
currPos.setCurrentLocation(currentLocation);
}
public String getProvider() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String Provider = locationManager.getBestProvider(criteria, true);
return Provider;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(getProvider(), 100, 1, locationListener);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(locationListener);
}
}
,我也有問題,drawCurrPosition
(當代碼「被工作」)功能(使用自定義的氣球),方法把標記上最後已知位置,而不是在我目前的位置(所以標記總是退一步)。 函數僅在打開的應用程序中將標記放在當前位置。
請你能幫助我,我看不出有什麼錯我的代碼
從這個問題的答案,請參閱... 的http:// stackoverflow.com/questions/9586530/positing-user-current-location-and-show-it-in-google-maps/9596183#9596183 – Prem 2012-03-22 06:05:56
謝謝,但仍然不工作=(也許我做錯了什麼,但我可以'看看是什麼,我得到錯誤無法到達工廠客戶端,我得到數字表格時間,並且沒有其他顯示 – hyperN 2012-03-22 11:53:13