How To Assign Current Geo-coordinates To Draw Path From Current Location To Target Location?
I am able to draw path between two sets of geo-coordinates from reference of j2memaprouteprovider. I am able to get current location latitude and longitude. How do i implement thes
Solution 1:
Its very Simple---
Make your Activity like this:--
publicclassGoogleMapLocationActivityextendsMapActivity {
private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;
private MapView myMapView;
private MapController myMapController;
LinearLayout zoomLayout;
GeoPoint myLastPosition;
AddItemizedOverlay mapOvlay;
privatevoidCenterLocatio(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);
List<Overlay> mapOverlays = myMapView.getOverlays();
Drawabledrawable=this.getResources().getDrawable(R.drawable.map_point);
if(myLastPosition != null){
mapOvlay = newAddItemizedOverlay(myLastPosition ,centerGeoPoint,drawable );
OverlayItemoverlayitem=newOverlayItem(centerGeoPoint,"","" );
mapOvlay.addOverlay(overlayitem);
mapOverlays.add(mapOvlay);
}
myLastPosition = centerGeoPoint;
};
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MapView)findViewById(R.id.mapview);
myLastPosition = null;
myMapController = myMapView.getController();
myMapController.setZoom(18); //Fixed Zoom Level
myLocationManager = (LocationManager)getSystemService(
Context.LOCATION_SERVICE);
myLocationListener = newMyLocationListener();
myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);
privateclassMyLocationListenerimplementsLocationListener{
publicvoidonLocationChanged(Location argLocation) {
GeoPointmyGeoPoint=newGeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));
GeoPointnewGeoPoint=newGeoPoint(myGeoPoint.getLatitudeE6() ,myGeoPoint.getLongitudeE6());
CenterLocatio(newGeoPoint);
}
publicvoidonProviderDisabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Disabled" ,Toast.LENGTH_SHORT).show();
}
publicvoidonProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Enabled" ,Toast.LENGTH_SHORT).show();
}
publicvoidonStatusChanged(String provider,
int status, Bundle extras) {
Toast.makeText(getBaseContext(),"GPS Unavailable" ,Toast.LENGTH_SHORT).show();
}
}
@OverrideprotectedbooleanisRouteDisplayed() {
returnfalse;
};
}
AddItemizedOverlay.class-----
publicclassAddItemizedOverlayextendsItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = newArrayList<OverlayItem>();
private Context context;
private GeoPoint mGpt1;
private GeoPoint mGpt2;
publicAddItemizedOverlay(GeoPoint gp1,GeoPoint gp2, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
mGpt1 = gp1;
mGpt2 = gp2;
}
publicAddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
publicAddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Overrideprotected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Overridepublicintsize() {
return mapOverlays.size();
}
@OverrideprotectedbooleanonTap(int index) {
Log.e("Tap", "Tap Performed");
returntrue;
}
publicvoidaddOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
publicbooleandraw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = newPaint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Pointpt1=newPoint();
Pointpt2=newPoint();
Projectionprojection= mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
returntrue;
}
}
Hope it will help you...
Solution 2:
Use following, It will display direction on native map application,
finalIntentintent=newIntent(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);
Post a Comment for "How To Assign Current Geo-coordinates To Draw Path From Current Location To Target Location?"