开发者

android: mapView doesnt give me correct user position

开发者 https://www.devze.com 2023-02-11 11:51 出处:网络
As topic I\'m currently in sweden but when launching my map it shows that I\'m in England. My code looks like this:

As topic I'm currently in sweden but when launching my map it shows that I'm in England.

My code looks like this:

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);
}

}

I've added........

    <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>

..... in my manifest.

I'm using http://developer.android.com/guide/topics/location/obtaining-user-location.html as a guide.

Very thankful for help!


You are actually creating the geo point wrong. You are setting the latitude and altitude here.

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

You'll want to set the latitude and longitude

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

Also try checking out the MyLocationOverlay class


it is converting to an int before it does the multiplication so you need parens around the calculation. Replace with this.

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

You also might want to check out MyLocationOverlay if you want to draw your location on a map.

To use MyLocationOverlay.

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

In on pause you should also call myLocationOverlay.disableMyLocation() so it doesn't drain battery when your activity is in the background.

0

精彩评论

暂无评论...
验证码 换一张
取 消