我在android開發方面經驗不足,我想在地圖上顯示當前位置。但它顯示了我在加拿大不同的位置。這裏是我的代碼:android gps如何顯示當前位置
package com.manita.mapuse;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MapuseActivity extends MapActivity implements LocationListener {
private MapView mapView = null;
private MapController mc;
private GeoPoint location;
double lat;
double lon;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---use the LocationManager class to obtain GPS locations---
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
lat= loc.getLatitude();
lon= loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
} /*End of Class MyLocationListener */
/* class map view */
public MapView viewmap (View mapview, double latitude, double longitude){
this.mapView = new MapView(this,this.getResources().getString(R.string.mapKey));
this.mapView.setClickable(true);
this.mc = this.mapView.getController();
this.location = new GeoPoint((int) (latitude * 1000000.0),(int) (longitude * 1000000.0));
this.mc.setCenter(this.location);
this.mc.setZoom(17);
this.mapView.setSatellite(true);
this.mapView.invalidate();
this.setContentView(this.mapView);
mapView.setBuiltInZoomControls(true);
return mapView;
}
/* End class map view */
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@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
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}