2011-02-17 31 views
0

作为主题我目前在瑞典,但是当启动我的地图时,它显示我在英格兰。android:mapView不给我正确的用户位置

我的代码如下所示:

private MapListener myMapViewListener; 
private MapController mapController; 
private GeoPoint test; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.mapview); 
    setupListener(); 

    MapView mapView = (MapView) findViewById(R.id.mapview); 
    mapController = mapView.getController(); 
    mapView.setBuiltInZoomControls(true); 
    mapView.setClickable(true); 

    mapController.setZoom(7); // defualt zoom  
} 

private void setupListener() { 

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    MapListener listener = new MapListener(); 
    MapView mapView = (MapView) findViewById(R.id.mapview); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000); 
      mapController.animateTo(test); 
     } 

     @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 

     } 
    }; 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
} 

}

我已经添加了........

<uses-library android:name="com.google.android.maps" /> 
</application> 

<uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses- permission> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

    </manifest> 

.....我的清单。

我使用http://developer.android.com/guide/topics/location/obtaining-user-location.html作为指南。

非常感谢您的帮助!

回答

3

它在进行乘法运算之前将其转换为int,因此您需要计算周围的参数。 用此替换。

test = new GeoPoint((int)(location.getLatitude()*1000000), 
    (int)(location.getLongitude()*1000000)); 

你可能也想看看MyLocationOverlay,如果你想在地图上画出您的位置。

使用MyLocationOverlay。

MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView); 
mapView.getOverlays().add(myLocationOverlay); 
myLocationOverlay.enableMyLocation(); 

在上暂停,你也应该调用myLocationOverlay.disableMyLocation(),所以当你的活动是在后台不会耗尽电池。

+0

没有工作,我甚至尝试:\t \t \t双LAT =位置。 getLatitude()* 1000000; \t \t \t double alt = location.getAltitude()* 1000000; \t \t \t test = new GeoPoint((int)lat,(int)alt); \t \t mapController.animateTo(test); – 2011-02-17 20:05:34

+0

@ Gwing-Ben Yuen您正在使用高度而不是经度。 – 2011-02-17 20:06:44

4

您实际上是在创建地理点错误的。您正在设置纬度和高度。

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getAltitude()*1000000) 

你要设置的纬度和经度

test = new GeoPoint((int)location.getLatitude()*1000000, (int)location.getLongitude()*1000000) 

也可尝试检查出MyLocationOverlay