plot a route on google maps, android - android

I want to draw a route on google map with the change in my position using GPS. As my location changes(when new geopoints are created), the dot moves on the google map but i'm unable to draw the line on the map.
Please help in plotting the route on google maps. Below is my code
`
LocationManager locman;
LocationListener loclis;
Location location;
private MapView map;
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locman.requestLocationUpdates(provider,60000, 100,loclis);
//Location = locman.getLastKnownLocation(provider);
}
/** Find and initialize the map view. */
private void initMapView() {
map = (MapView) findViewById(R.id.mapView);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}
/** Find Current Position on Map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(16);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
public void onLocationChanged(Location location) {
if (location != null){
lat = location.getLatitude();
lon = location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
controller.animateTo(New_geopoint);
}
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
Projection projection = map.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
canvas.drawLine(from.x, from.y, to.x, to.y, paint);
}
}
}
`

Related

How to make anything when my geolocation is changed?

I want to make some action, i.e. show dialog with text 'im here' every time when my geolocation will be changed.
I'm using MyLocationOverlay to draw a dot on my location on the Google Maps's mapview.
How can I achieve that?
Below is the location listener class -
private MyOverLay draw = new MyOverLay();
class MyLocationListener_gps implements LocationListener {
public void onLocationChanged(Location location) {
clat = location.getLatitude();
clon = location.getLongitude();
GeoPoint geoPoint = new GeoPoint((int) (clat * 1E6),
(int) (clon * 1E6));
mapView.getController().animateTo(geoPoint);
draw = new MyOverLay(geoPoint);
mapView.getOverlays().add(draw);
}
}
Here is code overlay class-
class MyOverLay extends Overlay {
private GeoPoint gp1;
private int mRadius = 3;
public MyOverLay() {
}
public MyOverLay(GeoPoint gp1) {
this.gp1 = gp1;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(Color.BLUE);
RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
canvas.drawOval(oval, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
}
It will draw a blue dot as u move and ur co-ordinates chnage.
Override the onLocationChanged method:
// Create an overlay to show current location
mMyLocationOverlay = new MyLocationOverlay(this, mMapView) {
#Override
public void onLocationChanged(android.location.Location location) {
String text = "New coordinates: " + location.getLatitude() + ", " + location.getLatitude();
Toast toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
toast.show();
super.onLocationChanged(location);
}
};
mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});
mMapView.getOverlays().add(mMyLocationOverlay);

HowTo OnTouch on a paint overlay (Google maps)?

I paint a overlay on my Google maps view (closed polygon, filled). But now I don't know how to pop up a toast when I tap on the overlay. The most examples which I found work with marker and looks very different to my code.
Main Activity:
public class BOSLstItemDetail extends MapActivity{
ArrayList<HashMap<String, Object>> boslst;
MapView mapView;
MapController mapcontrol;
GeoPoint p;
Polygon polygon;
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boslst_item_detail);
String coordinates[] = {"48.098056", "9.788611"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
MapView mapView = (MapView) findViewById(R.id.mapview);
mapcontrol = mapView.getController();
mapcontrol.animateTo(p);
mapcontrol.setZoom(10);
mapView.setBuiltInZoomControls(true);
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
InputStream koord = getAssets().open("gps.txt");
if (koord != null) {
InputStreamReader input = new InputStreamReader(koord);
BufferedReader buffreader = new BufferedReader(input);
String line;
while (( line = buffreader.readLine()) != null) {
String[] point_t = line.split(",");
double y = Double.parseDouble(point_t[0]);
double x = Double.parseDouble(point_t[1]);
points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
}
koord.close();
polygon = new Polygon(points);
}
}catch (Exception e) {
Log.e("APP","Failed", e);
}
mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
}
Polygon.java
public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
public Polygon(ArrayList<GeoPoint> points){
geoPoints = points;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow){
//Set the color and style
Paint paint = new Paint();
paint.setColor(Color.parseColor("#88ff0000"));
paint.setAlpha(50);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//Create path and add points
Path path = new Path();
Point firstPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
path.moveTo(firstPoint.x, firstPoint.y);
for(int i = 1; i < geoPoints.size(); ++i){
Point nextPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
path.lineTo(nextPoint.x, nextPoint.y);
}
//Close polygon
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);
}
}
gps.txt:
9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.87827968597418,48.2786293029785
9.87261867523205,48.2822494506837
9.87254810333263,48.2859611511232
9.88368034362787,48.2898597717285
9.8835382461549,48.2972793579102
9.72781181335461,47.9827613830566
9.72225093841558,47.9826812744141
9.72232818603527,47.9789619445801
9.71129894256597,47.9750900268555
9.70574092864985,47.9750099182129
9.70557022094732,47.9824409484864
9.69992923736572,47.9860801696778
9.69436073303234,47.9860000610352
9.33546066284174,48.2403602600099
9.34669876098644,48.2405319213867
If you're extending Overlay, you can override the OnTap method:
protected boolean onTap(final int index)
{
OverlayItem item = mOverlays.get(index);
if(item != null){
Toast.makeText(mContext, textToShow, Toast.LENGTH_SHORT).show();
}
//return true to indicate we've taken care of it
return true;
}

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 these coordinates in the following class to get path drawn from current position to target position.
MapRouteActivity.java
public class MapRouteActivity extends MapActivity {
LinearLayout linearLayout;
MapView mapView;
private Road mRoad;
static Context context = null;
String GPSPROVIDER = LocationManager.GPS_PROVIDER;
private static final long MIN_GEOGRAPHIC_POOLING_TIME_PERIOD = 10000;
private static final float MIN_GEOGRAPHIC_POOLING_DISTANCE = (float) 5.0;
public LocationManager gpsLocationManager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
context = this;
/* Get a listener for GPS */
LocationListener gpsLocationListener = null;
gpsLocationListener = new GpsLocationListener(this);
/* Start Location Service for GPS */
gpsLocationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
/* Register GPS listener with Location Manager */
gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
if (gpsLocationManager == null) {
gpsLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_GEOGRAPHIC_POOLING_TIME_PERIOD,
MIN_GEOGRAPHIC_POOLING_DISTANCE, gpsLocationListener);
}
boolean isAvailableGps = gpsLocationManager
.isProviderEnabled(GPSPROVIDER);
if (isAvailableGps) {
Location loc = gpsLocationManager.getLastKnownLocation("gps");
if (loc != null) {
double lattitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(
MapRouteActivity.this,
"Longitude iss " + lattitude + " Latitude iss "
+ longitude, Toast.LENGTH_LONG).show();
}
}
new Thread() {
#Override
public void run() {
double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
}
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
TextView textView = (TextView) findViewById(R.id.description);
textView.setText(mRoad.mName + " " + mRoad.mDescription);
MapOverlay mapOverlay = new MapOverlay(mRoad, mapView);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
};
};
private InputStream getConnection(String url) {
InputStream is = null;
try {
URLConnection conn = new URL(url).openConnection();
is = conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public static class GpsLocationListener implements LocationListener {
private ImageView mCurrentPointer;
public GpsLocationListener(Context context) {
}
public void onLocationChanged(Location loc) {
if (loc != null) {
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
GeoPoint point = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
Toast.makeText(
context,
"Longitude is " + longitude + " Latitude is "
+ latitude, Toast.LENGTH_LONG).show();
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
class MapOverlay extends com.google.android.maps.Overlay {
Road mRoad;
ArrayList<GeoPoint> mPoints;
public MapOverlay(Road road, MapView mv) {
mRoad = road;
if (road.mRoute.length > 0) {
mPoints = new ArrayList<GeoPoint>();
for (int i = 0; i < road.mRoute.length; i++) {
mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1000000),
(int) (road.mRoute[i][0] * 1000000)));
}
int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
mPoints.size() - 1).getLatitudeE6() - mPoints.get(0)
.getLatitudeE6()) / 2);
int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
mPoints.size() - 1).getLongitudeE6() - mPoints.get(0)
.getLongitudeE6()) / 2);
GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);
MapController mapController = mv.getController();
mapController.animateTo(moveTo);
mapController.setZoom(7);
}
}
#Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
super.draw(canvas, mv, shadow);
drawPath(mv, canvas);
return true;
}
public void drawPath(MapView mv, Canvas canvas) {
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
}
I want to assign current position lattiude and longitude value to fromLat and fronLon variable in above code here:
new Thread() {
#Override
public void run() {
double fromLat = 49.85, fromLon = 24.016667, toLat = 50.45, toLon = 30.523333;
String url = RoadProvider
.getUrl(fromLat, fromLon, toLat, toLon);
InputStream is = getConnection(url);
mRoad = RoadProvider.getRoute(is);
mHandler.sendEmptyMessage(0);
}
}.start();
Help !!
Its very Simple---
Make your Activity like this:--
public class GoogleMapLocationActivity extends MapActivity {
private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLongitude, myLatitude;
private MapView myMapView;
private MapController myMapController;
LinearLayout zoomLayout;
GeoPoint myLastPosition;
AddItemizedOverlay mapOvlay;
private void CenterLocatio(GeoPoint centerGeoPoint)
{
myMapController.animateTo(centerGeoPoint);
List<Overlay> mapOverlays = myMapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.map_point);
if(myLastPosition != null){
mapOvlay = new AddItemizedOverlay(myLastPosition ,centerGeoPoint,drawable );
OverlayItem overlayitem = new OverlayItem(centerGeoPoint,"","" );
mapOvlay.addOverlay(overlayitem);
mapOverlays.add(mapOvlay);
}
myLastPosition = centerGeoPoint;
};
/** Called when the activity is first created. */
#Override
public void onCreate(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 = new MyLocationListener();
myLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
myLocationListener);
private class MyLocationListener implements LocationListener{
public void onLocationChanged(Location argLocation) {
GeoPoint myGeoPoint = new GeoPoint(
(int)(argLocation.getLatitude()*1000000),
(int)(argLocation.getLongitude()*1000000));
GeoPoint newGeoPoint = new GeoPoint(myGeoPoint.getLatitudeE6() ,myGeoPoint.getLongitudeE6());
CenterLocatio(newGeoPoint);
}
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Disabled" ,Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Enabled" ,Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider,
int status, Bundle extras) {
Toast.makeText(getBaseContext(),"GPS Unavailable" ,Toast.LENGTH_SHORT).show();
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
};
}
AddItemizedOverlay.class-----
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
private GeoPoint mGpt1;
private GeoPoint mGpt2;
public AddItemizedOverlay(GeoPoint gp1,GeoPoint gp2, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
mGpt1 = gp1;
mGpt2 = gp2;
}
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
Log.e("Tap", "Tap Performed");
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
super.draw(canvas, mapView, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
Point pt1 = new Point();
Point pt2 = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(mGpt1, pt1);
projection.toPixels(mGpt2, pt2);
canvas.drawLine(pt1.x, pt1.y, pt2.x, pt2.y, paint);
return true;
}
}
Hope it will help you...
Use following, It will display direction on native map application,
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);

How do you draw a line from previous point to current point in Android Google Maps?

I am trying to create a GPS tracking app, and is stuck on how I can draw a line from the previous point to current point. I've tried using an Overlay, but it does not display... I am not THAT good on Java, so please speak to me like I'm 4 years old...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
TabHost.TabSpec spec;
TabHost th = (TabHost)findViewById(R.id.tabhost);
th.setup();
spec = th.newTabSpec("tag1");
spec.setContent(R.id.mapTab);
spec.setIndicator("Map");
th.addTab(spec);
spec = th.newTabSpec("tag2");
spec.setContent(R.id.logTab);
spec.setIndicator("Log");
th.addTab(spec);
spec = th.newTabSpec("tag3");
spec.setContent(R.id.detailsTab);
spec.setIndicator("Details");
th.addTab(spec);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//Map and Controls
private void initMapView() {
map = (MapView) findViewById(R.id.mvMain);
controller = map.getController();
map.setSatellite(true);
//map.setStreetView(true);
map.setBuiltInZoomControls(true);
}
//Creates an Overlay that marks current position
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass();
overlay.runOnFirstFix(new Runnable() {
public void run() {
controller.setZoom(17);
controller.animateTo(overlay.getMyLocation());
map.getOverlays().add(overlay);
}
});
}
//Experiment
public class detailsTab extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.detailsTab);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.detailsText);
if(location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong" + lng;
}
else {
latLongString = "No Location Found";
}
myLocationText.setText("Your current position is: \n" + latLongString);
}
}
public class NewOverlay extends Overlay {
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Double lat = lati *1E6;
Double lng = longi *1E6;
GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());
if (shadow == false) {
Point myPoint = new Point();
projection.toPixels(geoPoint, myPoint);
//Creating and setting up the paint brush
Paint paint = new Paint();
paint.setARGB(250, 255, 0, 0);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
//Create circle
int rad = 25;
RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);
canvas.drawOval(oval, paint);
canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint);
}
}
}
}
I do that this way, inside draw() method,
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.rgb(0x7b, 0x7b, 0xff));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
if(mPoints!=null || mPoints.size()<2)
{
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
Point loc = new Point();
mv.getProjection().toPixels(new GeoPoint((int) (location.getLatitude()*1.0E6),(int) (location.getLongitude()*1.0E6)), loc);
x2 = point.x;
y2 = point.y;
if (i == 0)
{
x2 = loc.x;
y2 = loc.y;
}
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
This code will draw for me a whole route, mPoints is an array of GeoPoint that I want to draw them. This should be useful for you.
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Projection projection = mapv.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
p.lineTo(to.x, to.y);
}
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
canvas.drawPath(p, mPaint);
mapv.invalidate();
super.draw(canvas, mapv, shadow);
}//draw()
we can use the above method to draw route as we move place to place. where geoPointsArray is array of locations received. And in your onCreate() you need to call mapv.invalidate(); to draw route continously

Can't get geopoint when using class MyLocationOverlay

I can't marker current location... Apps always get messeger "Cannot determine location".
I run app on Samsung galaxy mini and i was turn on GPS :(
Class FixedMyLocationOverlay
public class FixedMyLocationOverlay extends MyLocationOverlay {
private boolean bugged = false;
private Drawable drawable;
private Paint accuracyPaint;
private Point center;
private Point left;
private int width;
private int height;
public FixedMyLocationOverlay(Context context, MapView mapView) {
super(context, mapView);
}
#Override
protected void drawMyLocation(Canvas canvas, MapView mapView,
Location lastFix, GeoPoint myLocation, long when) {
if(!bugged) {
try {
super.drawMyLocation(canvas, mapView, lastFix, myLocation, when);
} catch (Exception e) {
// we found a buggy phone, draw the location icons ourselves
bugged = true;
}
}
if(bugged) {
if(drawable == null) {
accuracyPaint = new Paint();
accuracyPaint.setAntiAlias(true);
accuracyPaint.setStrokeWidth(2.0f);
drawable = mapView.getContext().getResources().getDrawable(R.drawable.here);
width = drawable.getIntrinsicWidth();
height = drawable.getIntrinsicHeight();
center = new Point();
left = new Point();
}
Projection projection = mapView.getProjection();
double latitude = lastFix.getLatitude();
double longitude = lastFix.getLongitude();
float accuracy = lastFix.getAccuracy();
float[] result = new float[1];
Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
float longitudeLineDistance = result[0];
GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-accuracy/longitudeLineDistance)*1e6));
projection.toPixels(leftGeo, left);
projection.toPixels(myLocation, center);
int radius = center.x - left.x;
accuracyPaint.setColor(0xff6666ff);
accuracyPaint.setStyle(Style.STROKE);
canvas.drawCircle(center.x, center.y, radius, accuracyPaint);
accuracyPaint.setColor(0x186666ff);
accuracyPaint.setStyle(Style.FILL);
canvas.drawCircle(center.x, center.y, radius, accuracyPaint);
drawable.setBounds(center.x - width/2, center.y - height/2, center.x + width/2, center.y + height/2);
drawable.draw(canvas);
}
}
}
Code Activity
public class atm_atmogan extends MapActivity {
private MapView mapView;
private MyLocationOverlay myLocationOverlay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.atm_atmogan);
mapView = (MapView) findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
myLocationOverlay = new FixedMyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
mapView.postInvalidate();
zoomToMyLocation();
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
}
#Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
}
private void zoomToMyLocation() {
GeoPoint myLocationGeoPoint = myLocationOverlay.getMyLocation();
if(myLocationGeoPoint != null) {
mapView.getController().animateTo(myLocationGeoPoint);
mapView.getController().setZoom(18);
}
else {
Toast.makeText(this, "Cannot determine location", Toast.LENGTH_SHORT).show();
}
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
You will need to get your actual location using the location Manager class/interface.
This tutorial will cover everything you need to know about that http://www.vogella.de/articles/AndroidLocationAPI/article.html
hope this helps

Categories

Resources