我使用GPS来改变我的位置,然后在地图上添加叠加以显示我的位置,直到现在如果我改变我的位置图标的位置改变,但在我添加另一个叠加以显示其他图标(标记)如果我改变了地方,我的地方的图标不能改变?!为什么??
编辑:
package com.adhamenaya.android;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class MapApp extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private GeoPoint p;
List<Overlay> listOfOverlays ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initLayout();
initMap();
listOfOverlays = mapView.getOverlays();
}
private void initLayout(){
mapView = (MapView) findViewById(R.id.mapview);
}
private void initMap(){
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController=mapView.getController();
mapController.setZoom(10);// 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class MapOverlay extends com.google.android.maps.Overlay
{
private GeoPoint p;
private int res;
public MapOverlay(GeoPoint p,int res){
this.p=p;
this.res=res;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(),res);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-20, null);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
mapController.animateTo(p);
MapOverlay mapOverlay = new MapOverlay(p,R.drawable.redicon);
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
return false;
}
}
class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
int lat=(int)(location.getLatitude()*1E6);
int lng=(int)(location.getLongitude()*1E6);
GeoPoint p=new GeoPoint(lat,lng);
mapController.animateTo(p);
MapOverlay mapOverlay = new MapOverlay(p,R.drawable.blueicon);
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}