possible to highlight a section of a street?

喜你入骨 提交于 2019-12-11 23:33:26

问题


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?


回答1:


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});
}


来源:https://stackoverflow.com/questions/7699173/possible-to-highlight-a-section-of-a-street

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!