1

So i've been building an app using Google Maps API and i've run into a wall..

So far i got the MapView (obviously) and i've set a GeoPoint marking the final destination of the wanted route.

I'm in the process of adding GPS funcionality to the code so i can listen for my actual position and change as i move. Its still not implemented but i know how to. I also added an image to use as pin, to pinpoint the final destination.

What i don't know is how do i connect the two points, with a route. If someone could enlighten me on that, it would be great

What i got so far:

package com.example.igestao;

import android.os.Bundle;
import android.annotation.SuppressLint;
import com.example.igestao.R;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapController;
import com.google.android.maps.GeoPoint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.Overlay;
import java.util.List;

@SuppressLint("SetJavaScriptEnabled")
public class Mapa extends MapActivity {

    MapView map;
    MapController mc;
    GeoPoint p;

    class MapOverlay extends com.google.android.maps.Overlay {

        @Override
        public boolean draw(Canvas canvas, MapView map, boolean shadow, long when) {

            super.draw(canvas, map, shadow);

            //translate the GeoPoint to screen pixels
            Point screenPts = new Point();
            map.getProjection().toPixels(p, screenPts);

            //add pin
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
            return true;        
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        map = (MapView)findViewById(R.id.mvMapa);
        map.setBuiltInZoomControls(true);
        //map.setSatellite(true);
        mc = map.getController();
        String coordinates[] = {"38.75795","-9.15324"};
        double lat = Double.parseDouble(coordinates[0]);
        double lon = Double.parseDouble(coordinates[1]);
        p = new GeoPoint(   (int)(lat * 1E6), (int)(lon * 1E6) );
        mc.animateTo(p);
        mc.setZoom(17);
        map.invalidate();

        //add location marker

        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        map.invalidate();        
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
jsfrocha
  • 1,812
  • 2
  • 21
  • 32

1 Answers1

0

I have had similar success with a solution based on parsing the directions via a HTTP Request to Google Maps (specifically, its directions service), as described in this excellent answer. This explains all the necessary steps to get/parse and draw the route on your Map Activity. Hope this helps!

Community
  • 1
  • 1
Dr1Ku
  • 2,875
  • 3
  • 47
  • 56