2011-05-20 68 views
-3
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; 
} 
+2

這只是普通的懶惰。 – 2011-05-20 01:39:17

回答

相關問題