how Can updated current user position in Google map . when i run the below pgm first time it gives me the mycurrent position only [closed]
public class ABC extends MapActivity
{
private LocationManager lm;
private LocationListener locationlistener;
private MapController mapController;
private MapView mapView;
GeoPoint initGeoPoint = null;;
double  lat, lon;
MyLocationOverlay myLocationOverlay =null;
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.popup1); 
    mapView=(MapView) findViewById(R.id.mapview);
    lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationlistener=new MyLocationlistener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistener); 
    lat= lm.getLastKnownLocation (LocationManager.GPS_PROVIDER) .getLatitude();    
    lon= lm.getLastKnownLocation (LocationManager.GPS_PROVIDER).getLongitude(); 
    System.out.println("lat"+lat);
    initGeoPoint = new GeoPoint((int)(lat*1E6), (int)(lon*1E6));
    mapController=mapView.getController();
    mapController.setCenter(initGeoPoint);
    mapController.setZoom(18);
    mapView.setStreetView(true);
    myLocationOverlay = new MyLocationOverlay();   
    List<Overlay> list = mapView.getOverlays();
    list.add(myLocationOverlay);
    // myLocationOverlay.enableMyLocation();
    mapController.animateTo(initGeoPoint);   
    mapView.invalidate();
}
@Override
public void onResume() {
    super.onResume();
}
protected class 开发者_运维知识库MyLocationOverlay extends com.google.android.maps.Overlay {
    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        Paint paint = new Paint();
        super.draw(canvas, mapView, shadow);
        // Converts lat/lng-Point to OUR coordinates on the screen.
        Point myScreenCoords = new Point();
        System.out.println("**************"+initGeoPoint);
        mapView.getProjection().toPixels(initGeoPoint, myScreenCoords);
        paint.setStrokeWidth(1);
        paint.setARGB(255, 255, 255, 255);
        paint.setStyle(Paint.Style.STROKE);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.icon1);
        canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
        canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
        return true;
    }
}
class MyLocationlistener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            double lat = loc.getLatitude();
            double lng = loc.getLongitude();
            System.out.println("*******latitude"+lat);
            System.out.println("******longitude"+lng);
            initGeoPoint = new GeoPoint((int) (lat * 1E6),(int) (lng * 1E6));
            mapController.animateTo(initGeoPoint);
            Toast.makeText(getBaseContext(), "New location latitude [" +
                             lat + "] longitude [" + lng +"]",
                             Toast.LENGTH_SHORT).show(); 
            mapView.invalidate();
        }        
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status,Bundle extras) {
    }
}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
use the updates listener you can register for updates via the requestLocationUpdates. Do read the documentations for more details
http://developer.android.com/reference/android/location/LocationManager.html#PROVIDERS_CHANGED_ACTION
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论