I\'ve been busy for a long time finding out how to draw a line between two (GPS) points on the map in HelloMapView but with no luck.
Could anyone please tell me how
For those who really only want to draw a simple line - there is indeed also the short short version.
GoogleMap map;
// ... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
from https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polyline
This worked for me. With the method mentioned here I was able to draw polylines on Google Maps V2. I drew a new line whenever the user location got changed, so the the polyline looks like the path followed by user on map.
Source code at. Github: prasang7/eTaxi-Meter
Please ignore other modules of this project related to distance calculation and User Interface if you are not interested in them.
This can be done by using intents too:
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(
"http://maps.google.com/maps?" +
"saddr="+YOUR_START_LONGITUDE+","+YOUR_START_LATITUDE+"&daddr="YOUR_END_LONGITUDE+","+YOUR_END_LATITUDE));
intent.setClassName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
Yes you need to use overlays.
You need to get the MapView's overlays and add your new overlay onto it.
Your class extends Overlay, which is a transparent canvas in which you can draw on it like any other canvas.
You can use mapView.getProjection() to get projection of map view.
...
More info found here: http://blogoscoped.com/archive/2008-12-15-n14.html
just i will find draw with some rectangle in mapview
just we want change paint as we like
EmptyOverlay.java
public class EmptyOverlay extends Overlay {
private float x1,y1;
private MapExampleActivity mv = null;
private Overlay overlay = null;
public EmptyOverlay(MapExampleActivity mapV){
mv = mapV;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
return super.draw(canvas, mapView, shadow, when);
}
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if(mv.isEditMode()){
if(e.getAction() == MotionEvent.ACTION_DOWN){
//when user presses the map add a new overlay to the map
//move events will be catched by newly created overlay
x1 = y1 = 0;
x1 = e.getX();
y1 = e.getY();
overlay = new MapOverlay(mv, x1, y1);
mapView.getOverlays().add(overlay);
}
if(e.getAction() == MotionEvent.ACTION_MOVE){
}
//---when user lifts his finger---
if (e.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
return false;
}
}
MapExampleActivity.java
public class MapExampleActivity extends MapActivity {
private MapView mapView;
private boolean isEditMode = false;
private Button toogle;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toogle = (Button)findViewById(R.id.toogleMap);
toogle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
toogleEditMode();
}
});
mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true); //display zoom controls
//add one empty overlay acting as a overlay loader. This will catch press events and will add the actual overlays
mapView.getOverlays().add(new EmptyOverlay(this));
mapView.postInvalidate();
}
//toogle edit mode for drawing or navigating the map
private void toogleEditMode(){
isEditMode = !isEditMode;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected boolean isLocationDisplayed() {
return false;
}
public boolean isEditMode(){
return this.isEditMode;
}
public MapView getMapView(){
return this.mapView;
}
}
MapOverlay.java
public class MapOverlay extends Overlay {
private float x1,y1,x2,y2;
private GeoPoint p1=null,p2=null;
private MapExampleActivity mv = null;
private Paint paint = new Paint();
private boolean isUp = false;
//constructor receiving the initial point
public MapOverlay(MapExampleActivity mapV,float x,float y){
paint.setStrokeWidth(2.0f);
x1 = x;
y1 = y;
mv = mapV;
p1 = mapV.getMapView().getProjection().fromPixels((int)x1,(int)y1);
}
//override draw method to add our custom drawings
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
if(p1 != null && p2 != null){
//get the 2 geopoints defining the area and transform them to pixels
//this way if we move or zoom the map rectangle will follow accordingly
Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
Point screenPts2 = new Point();
mapView.getProjection().toPixels(p2, screenPts2);
//draw inner rectangle
paint.setColor(0x4435EF56);
paint.setStyle(Style.FILL);
canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
//draw outline rectangle
paint.setColor(0x88158923);
paint.setStyle(Style.STROKE);
canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
if(mv.isEditMode() && !isUp){
if(e.getAction() == MotionEvent.ACTION_DOWN){
x1 = y1 = 0;
x1 = e.getX();
y1 = e.getY();
p1 = mapView.getProjection().fromPixels((int)x1,(int)y1);
}
//here we constantly change geopoint p2 as we move out finger
if(e.getAction() == MotionEvent.ACTION_MOVE){
x2 = e.getX();
y2 = e.getY();
p2 = mapView.getProjection().fromPixels((int)x2,(int)y2);
}
//---when user lifts his finger---
if (e.getAction() == MotionEvent.ACTION_UP) {
isUp = true;
}
return true;
}
return false;
}
}
see this http://n3vrax.wordpress.com/2011/08/13/drawing-overlays-on-android-map-view/
Try this one:
Add itemizedOverlay class:
public class AndroidGoogleMapsActivity extends MapActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Displaying Zooming controls
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapController mc = mapView.getController();
double lat = Double.parseDouble("48.85827758964043");
double lon = Double.parseDouble("2.294543981552124");
GeoPoint geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));
mc.animateTo(geoPoint);
mc.setZoom(15);
mapView.invalidate();
/**
* Placing Marker
* */
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.mark_red);
AddItemizedOverlay itemizedOverlay =
new AddItemizedOverlay(drawable, this);
OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello", "Sample Overlay item");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}