So lets start consider that you have already make one map simple application in android. And look at screen of simple map application result using your Map API Key
Now divide further task in steps----
Step 1) Create two point to draw on map using OverlayItem. After creating two Geo point, we will create one class ItemizedOverlayItem to populate these point on MapView
ItemizedOverlayItem itemMized;
Vector<OverlayItem> allOverLArrayList = new Vector<OverlayItem>();
private void drawTwoPoint() {
itemMized = new ItemizedOverlayItem(getResources().getDrawable(
R.drawable.ic_launcher));
GeoPoint geopoint = new GeoPoint(230286706, 72509145);
mapView.getController().setCenter(geopoint);
OverlayItem item = new OverlayItem(geopoint, "", "");
allOverLArrayList.add(item);
geopoint = new GeoPoint(222286706, 71509145);
item = new OverlayItem(geopoint, "", "");
allOverLArrayList.add(item);
itemMized.addOverLayItme(allOverLArrayList);
mapView.getOverlays().add(itemMized);
mapView.getController().setCenter(geopoint);
}
Step 2) So lets populate our two point on map. In next step, we will draw a most awaited path.
package com.ahmad.displaylineonmap;
import java.util.Vector;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class ItemizedOverlayItem extends ItemizedOverlay<OverlayItem> {
public ItemizedOverlayItem(Drawable arg0) {
super(boundCenter(arg0));
}
@Override
protected OverlayItem createItem(int arg0) {
return mlocItems.get(arg0);
}
public void addOverLayItme(Vector<OverlayItem> mlocItems) {
this.mlocItems = mlocItems;
populate();
}
Vector<OverlayItem> mlocItems;
@Override
public int size() {
return mlocItems.size();
}
}
Step 3) Now this tutorial creates a layer on map to draw a path between two Geo points.
package com.ahmad.displaylineonmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class OverLayClass extends Overlay {
Projection projection;
MapActivity mapActivity;
MapView mapView;
public OverLayClass(Projection projection, MapActivity act, MapView map) {
this.projection = projection;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
this.mapActivity = act;
mapView = map;
}
Paint mPaint;
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
GeoPoint geopoint = new GeoPoint(230286706, 72509145);
Point pointOne = projection.toPixels(geopoint, null);
geopoint = new GeoPoint(222286706, 71509145);
Point pointtwo = projection.toPixels(geopoint, null);
canvas.drawLine(pointOne.x, pointOne.y, pointtwo.x, pointtwo.y, mPaint);
}
}
Some points and lines need to be discusses here. One important thing is Projection which allows us to interchange device coordinate to Geo point. So look at this
GeoPoint geopoint = new GeoPoint(230286706, 72509145);
Point pointOne = projection.toPixels(geopoint, null);
geopoint = new GeoPoint(222286706, 71509145);
Point pointtwo = projection.toPixels(geopoint, null);
Attaching Layer name Overlay class to map to create any figure
OverLayClass mOverLayClass = new OverLayClass(projection, this, mapView);
mapView.getOverlays().add(mOverLayClass);
So now its time for complete source code
No comments:
Post a Comment
Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging