
This is the simplest code I have come up with for implementing Google map in Android.
package com.example.googlemap;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
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 GoogleMap extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private int lat;
private int lng;
GeoPoint p = new GeoPoint((int) (lat * 1000000), (int) (lng * 1000000));
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// create a map view
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(false);
mapController = mapView.getController();
mapController.setZoom(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L, 500.0f, new GeoUpdateHandler());
mapController.setCenter(p);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List
list.add(myLocationOverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
lat = (int) (location.getLatitude() * 1000000);
lng = (int) (location.getLongitude() * 1000000);
p = new GeoPoint(lat, lng);
mapController.animateTo(p); // mapController.setCenter(point);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(p, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.redpin);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y-38, paint);
//canvas.drawText("Here I am...", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
-----
You also need to to add the following code to your Menifest file
No comments:
Post a Comment