0

I need to highlight a section of a street between two intersections. I have found a similar question which was asked more than a year ago (see here) which says that Google, Bing, etc. do not provide street data through their APIs. Does anyone know if anything has changed in the APIs, and whether I can get street location latitude/longitude data from somewhere now?

Community
  • 1
  • 1
akonsu
  • 28,824
  • 33
  • 119
  • 194

1 Answers1

0

I have done this in API V2, but it should not be to difficult to rewrite it for V3.

Have a look at this website http://www.fvs.de/anfahrt.php (in German) and click the "Weg in Karte zeigen" buttons ("show way in map") in the sections below the map. Each button highlights another street line in the map.

It is basically done by using directions object of Google Maps to plot a route between two arbitrary street points in the map (e.g. your two intersections). The points must be encoded with their LatLng coordinates. Here is a code example (as said this is API V2):

function initMap() {  // called by page onload event
  if (GBrowserIsCompatible()) {
    // Display the map, with some controls and set the initial location 
    gMap = new GMap2(document.getElementById("map_canvas"));
    gMap.addControl(new GLargeMapControl());
    // ...
    gMap.setCenter(GLatLng(49.238326, 6.977761), 15);

    // init directions object and attach listener to handle route loads from function highliteRoute()
    gDir = new GDirections();
    gPoly = null;
    GEvent.addListener(gDir, 'load', function(){
      gPoly = gDir.getPolyline();
      gMap.addOverlay(gPoly);

      // zoom & pan to poly        
      var polyBds  = gPoly.getBounds();
      var polyZoom = gMap.getBoundsZoomLevel(polyBds);
      gMap.setZoom(polyZoom);
      gMap.panTo(polyBds.getCenter());
    });
  }
}

function highliteRoute(){
  if(gPoly!=null)  gPoly.hide();
  gDir.load('from: 49.313530,6.969109 to: 49.238326,6.977761', {getPolyline:true});
}
Jpsy
  • 20,077
  • 7
  • 118
  • 115
  • cool. thanks. I was thinking about getting walking directions from one intersection to another, but it is not guaranteed to return the path that uses the street that I want to highlight. I guess I will have to use Open Street Maps data to find street coordinates. – akonsu Oct 11 '11 at 00:31
  • I also faced the problem that the calculated directions did not always match the street that I wanted to highlight. This can be easily corrected by adding additional intermediate "to:" points to the search string, that force the result to use your street(s): `gDir.load('from: 49.313530,6.969109 to: 49.282163,7.010651 to: 49.238326,6.977761', {getPolyline:true});` – Jpsy Oct 11 '11 at 07:04
  • Oh, and to find the street coordinates: Use the "LatLng Marker" from Google Maps Labs: Open Google Maps, click the little gear icon in the upper right corner and choose "Maps Labs". Scroll down to the "LatLng Marker" and enable it and save. Back in the map do a right click on any location that you want to find the coordinates for and select "Drop LatLng Marker" from the context menu. You can even copy the coordinate values from the marker that appears: mark the coordinate text by quickly clicking three times into it, copy, paste. – Jpsy Oct 11 '11 at 07:09