Joining markers to draw path in android google map - android

I am a newbie in android field. I have written a android App1 that will retrieve latitude and longitude values from Network Provider and stores it in my local server(LAMP).
I have also created a MYSQL DB table that has 3 columns(lat,lon,id) that has the values (lat and lon) which are retrieved using the Network Provider. Currently there are more than 10 values in my table.
I have created JSON object for getting those values from MYSQL DB using PHP script in my Android App2. All these things works fine. I have also done creating MapActivity which will plot those lat and lon values on map using Marker.
What I have to do now is to join those markers to draw path on google map. How to do it. Please help

Try this.
String uri = "http://maps.google.com/maps?saddr=" + currentLatitude+","+currentLongitude+"&daddr="+fixedLatitude+","+fixedLongitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);
may this help you

Try this this to draw path in google map
public class Location extends MapActivity {
MapView mapView;
public static ArrayList<String> paramLat = new ArrayList<String>();
public static ArrayList<String> paramLong = new ArrayList<String>();
private List<Overlay> mapOverlays;
public List<GeoPoint> geopoints = new ArrayList<GeoPoint>();
public void onCreate(Bundle savedInstanceState) {
//your code to display location
for(int i=0;i<paramLat.size();i++)
{
lat = Double.parseDouble(paramLat.get(i));
lon = Double.parseDouble(paramLong.get(i));
geoPoint = new GeoPoint((int)(lat * 1E6), (int)(lon *1E6));
geopoints.add(geoPoint);
}
mapOverlays = mapView.getOverlays();
mapOverlays.add(new MyOverlay());
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
int loopcount = geopoints.size() - 1;
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
for (int i = 0; i < loopcount; i++)
{
GeoPoint pp1 = (GeoPoint) geopoints.get(i);
GeoPoint pp2 = (GeoPoint) geopoints.get(i + 1);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(pp1, p1);
projection.toPixels(pp2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
}
} //end of MyOverlay class
} //end of Location class

Related

MapView display route between a lot of GeoPoints

I have an app where an user is given a lot of points (100 or even more) and he has to physically go to those points and "check in". They have to go to those points in a certain order, so I need to display a route in the MapView which passes through all those points.
I've read a lot about getting the route between two points, but I can't find anything about drawing a complex route with a lot of points. Is this behavior possible?
public class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int color;
public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int color) {
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
}
Now all that's left now for our Overlay is to override the draw() method and draw the line as we need it:
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(color);
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
super.draw(canvas, mapView, shadow);
}
Back in the Activity, just iterate over the GeoPoints that you got from google maps and add each of them to the MapView:
private void drawPath(List geoPoints, int color) {
List overlays = mapView.getOverlays();
for (int i = 1; i < geoPoints.size(); i++) {
overlays.add(new RouteOverlay(geoPoints.get(i - 1), geoPoints.get(i), color));
}
}
Try something like this
if(DataSources.ActivitiesList.length >0)
{
String address = "http://maps.google.com/maps?daddr=" + DataSources.ActivitiesList[0].SiteLatitude.toString() + "," + DataSources.ActivitiesList[0].SiteLongitude.toString();
for (int i= 1 ;i < DataSources.ActivitiesList.length ; i++)
{
if(DataSources.ActivitiesList[i].SiteLatitude != null)
address += "+to:" + DataSources.ActivitiesList[i].SiteLatitude + "," + DataSources.ActivitiesList[i].SiteLongitude;
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(address));
startActivity(intent);
break;
}

How show multiple points on Google map?

When start Google Maps,I focus to a point on map as:
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6), (int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6), (int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.BLUE, mapView);
mapView.getController().animateTo(destGeoPoint);
If I use mapView.getController().animateTo(destGeoPoint); the map only focuses to point destGeoPoint.
I want Map to show 2 points: srcGeoPoint and destGeoPoint when start Google Map.
This 2 point had draw on Map. I want to can see 2 point when map open.
Create your mapOverlay:
public class MapOverlay extends Overlay {
MapView mapView;
GeoPoint p;
private Context c;
public MapOverlay(Context c, MapView m, GeoPoint p){
this.c=c;
mapView=m;
this.p=p;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(c.getResources(), R.drawable.icon_notif);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
This is a method that starts the map:
private void initMapa() {
try{
//If you want zoom controls
mapa.setBuiltInZoomControls(true);
GeoPoint loc;
Double lat;
Double long;
MapController controlMapa = mapa.getController();
controlMapa.setZoom(13); // de 1 a 21
mapa.setSatellite(true);
mapa.displayZoomControls(true);
// First point
ArrayList<GeoPoint> ap=new ArrayList<GeoPoint>();
latitud = (double) lat1 *1E6;
longitud = (double) long1 *1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
controlMapa.animateTo(loc);
ap.add(loc);
// Second point
latitud = (double) fichaOtraPersona.getLoc_lan()*1E6;
longitud = (double) fichaOtraPersona.getLoc_lon()*1E6;
loc = new GeoPoint(latitud.intValue(), longitud.intValue());
ap.add(loc);
controlMapa.animateTo(loc);
addPointInTheMap(ap);
} catch (Exception e) {
showToast("Error");
}
}
And the addPointInTheMap method:
private void anadirPuntoEnMapa(ArrayList<GeoPoint> points){
//---Add a location marker---
List<Overlay> listOfOverlays = mapa.getOverlays();
listOfOverlays.clear();
Iterator<GeoPoint> it=points.iterator();
while (it.hasNext()){
MapOverlay mapOverlay = new MapOverlay(getApplicationContext(), mapa, it.next());
listOfOverlays.add(mapOverlay);
}
mapa.invalidate();
}
Try to read the Location and Map Applications Chapter from the Android Cookbook. The main idea is to add an inner class to your MapActivity which extends ItemizedOverlay and implement the abstract methods and the default constructor. The ItemizedOverlay uses your implementations of the createItem and size() methods to get hold of all the overlay items in your implementation and do the aggregation.

Android google maps show geo points which are closest to current location

Is there any way If I have like 100 geo points added in my MapActivity and I want to show only these which are like 1000m away from my current location. The other ones should not be visible on the map.
Any suggestions?
Ok, here is the example. You can subclass Overlay class and override draw method. Whenever the Overlay is invalidated, you should check if your points are within 1000m from your location. Off course, you should know your location within your overlay class. The flowing is the example:
public class MyMapOverlay extends Overlay {
Bitmap bmp;
Context context;
public MyMapOverlay()
{
bmp = BitmapFactory.decodeResource(MyApplication.getContext().getResources(), R.drawable.myplace);
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
for(int i = 0;i<points.size(); i++)
{
GeoPoint point = points.get(i);
Location locationA = new Location("point " + String.valueOf(i));
locationA.setLatitude(point.getLatitudeE6());
locationA.setLongitude(point.getLongitudeE6());
float distance = myLocation.distanceTo(locationA);
if(distance < 1000.0)
{
mapView.getProjection().toPixels(p, screenPts);
canvas.drawBitmap(bmp, screenPts.x-bmp.getWidth()/2, screenPts.y-bmp.getHeight(), null);
}
}
return true;
}
With some help of the first answer I find this solution :
if(location != null){
Location locA = new Location("gps");
GeoPoint myLoc = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
mapController.animateTo(myLoc);
String sqlbars = "SELECT DISTINCT id, latitude, longitude, address, " +
" (SELECT category FROM bars_categories WHERE bar_id=bars.id) AS category " +
" FROM bars WHERE is_active=1";
Cursor curBars = dbHelper.executeSQLQuery(sqlbars);
if(curBars != null){
if(curBars.getCount() > 0){
for(curBars.move(0);curBars.moveToNext();curBars.isAfterLast()){
double latitude = curBars.getDouble(curBars.getColumnIndex("latitude"));
double longitude = curBars.getDouble(curBars.getColumnIndex("longitude"));
final List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = SofiaLiveNearbyActivity.this.getResources().getDrawable(R.drawable.pin_map_icon);
int markerWidth = drawable.getIntrinsicWidth();
int markerHeight = drawable.getIntrinsicHeight();
drawable.setBounds(0, markerHeight, markerWidth, 0);
final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, SofiaLiveNearbyActivity.this);
GeoPoint point = new GeoPoint((int)(latitude * 1E6),(int)(longitude *1E6));
OverlayItem overlayitem = new OverlayItem(point, "Type: "+category+"\n----------------\nAddress: "+address, id);
locA.setLatitude(latitude);
locA.setLongitude(longitude);
float distance = location.distanceTo(locA);
if(distance < 1000.0){
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
}
}
curBars.close();
}
and it worked.

plot a route on google maps, 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);
}
}
}
`

Drawing a line between 2 geopoints using Android 2.3

i am trying to draw a line between 2 geopoints. i am able to show to geopoints on the map.
Its working fine. but i am not able to draw a line between 2 points. program has no error but line is not getting displayed. can anyone tell me what i have to change.
public class HelloMapView extends MapActivity {
/** Called when the activity is first created. */
LinearLayout linearLayout;
MapView mapView;
MapController mc;
GeoPoint p,p1;
class MapOverlay extends com.google.android.maps.Overlay
{
#Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.a);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
//Coordinates 2
//---translate the GeoPoint to screen pixels---
Point screenPts1 = new Point();
mapView.getProjection().toPixels(p1, screenPts1);
//---add the marker---
Bitmap bmp1 = BitmapFactory.decodeResource(
getResources(), R.drawable.b);
canvas.drawBitmap(bmp1, screenPts1.x, screenPts1.y-50, null);
//----------- Start--------------//
Projection projection = mapView.getProjection();
Path path = new Path();
Point from = new Point();
Point to = new Point();
projection.toPixels(p, from);
projection.toPixels(p1, to);
path.moveTo(from.x, from.y);
path.lineTo(to.x, to.y);
Paint mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(0xFFFF0000);
mPaint.setAntiAlias(true);
canvas.drawPath(path,mPaint);
super.draw(canvas, mapView, shadow);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView = (MapView) findViewById(R.id.mapview);
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"12.958998", "77.658998"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
String coordinates1[] = {"12.95967","77.64918"};
double lat1 = Double.parseDouble(coordinates1[0]);
double lng1 = Double.parseDouble(coordinates1[1]);
p1 = new GeoPoint(
(int) (lat1 * 1E6),
(int) (lng1 * 1E6));
mc.animateTo(p);
mc.animateTo(p1);
mc.setZoom(16);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Forget all the Path stuff, just use the following line (it works for me):
canvas.drawLine(screenPts.x, screenPts.y, screenPts1.x, screenPts1.y, mPaint);
I suspect you need to change this line:
mPaint.setStyle(Style.FILL);
to:
mPaint.setStyle(Style.STROKE);
Also, while it's probably not related to your problem, it looks like you are calling super.draw twice, once at the beginning and once at the end. You probably just want to call it at the beginning.

Categories

Resources