- package com.example.mygooglemap;
- 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;
- import com.google.android.maps.Projection;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.RectF;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- public class MainActivity extends MapActivity implements LocationListener{
-
- static final int INITIAL_ZOOM_LEVEL = 20;
- static final int INITIAL_LATITUDE = 25040255;
- static final int INITIAL_LONGITUDE = 12152377;
-
- private MapView mapView;
- private MapController mapController;
- private LocationManager locationManager;
-
- private void updateMap(GeoPoint center)
- {
- MyPointOverlay myPointOverlay = new MyPointOverlay(center);
- mapView.getOverlays().clear();
- mapView.getOverlays().add(myPointOverlay);
- mapController.animateTo(center);
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mapView = (MapView)findViewById(R.id.mapview);
- mapView.setBuiltInZoomControls(true);
-
- GeoPoint center = new GeoPoint(INITIAL_LATITUDE,INITIAL_LONGITUDE);
-
- mapController = mapView.getController();
- mapController.setZoom(INITIAL_ZOOM_LEVEL);
- mapController.setCenter(center);
- updateMap(center);
-
- locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
- locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,0,this);
-
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- public void onLocationChanged(Location location) {
- // TODO Auto-generated method stub
- GeoPoint center = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
- updateMap(center);
- }
- public void onProviderDisabled(String provider) {
- // TODO Auto-generated method stub
-
- }
- public void onProviderEnabled(String provider) {
- // TODO Auto-generated method stub
-
- }
- 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;
- }
- }
- class MyPointOverlay extends Overlay
- {
- private GeoPoint geoPoint;
- private int mRadius = 7;
- private int sRadius = 5;
-
- public MyPointOverlay(GeoPoint gp)
- {
- geoPoint = gp;
- }
-
- @Override
- public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
- {
- Paint paint = new Paint();
- paint.setAntiAlias(true);
- paint.setColor(Color.RED);
-
- Point drawPoint = new Point();
- Projection projection = mapView.getProjection();
- projection.toPixels(geoPoint, drawPoint);
-
- RectF ovalPoint = new RectF(drawPoint.x-mRadius,
- drawPoint.y-mRadius,
- drawPoint.x+mRadius,
- drawPoint.y+mRadius);
-
- canvas.drawOval(ovalPoint, paint);
- paint.setColor(Color.BLUE);
- ovalPoint = new RectF(drawPoint.x - sRadius,
- drawPoint.y - sRadius,
- drawPoint.x + sRadius,
- drawPoint.y + sRadius);
- canvas.drawOval(ovalPoint, paint);
-
- return super.draw(canvas, mapView, shadow, when);
- }
-
- }
複製代碼 |