-1

真的需要一些想法。一直在使用Google地圖API,我正在使用地理編碼,反向地理編碼和地理位置。通過下面的這段代碼,我可以使用谷歌地圖的方向API,它完美地工作。谷歌方向API - 如何通過經度而不是物理地址

var locations = [ 
     {lat: 6.4261139, lng: 3.4363691} 
    ]; 

    var mapContainer = $('.map-container'), 
     map   = new google.maps.Map(mapContainer[0], { 
      center: center, 
      zoom : 10 
     }); 

    var markers = _(locations) 
     .map(function (loc) { 
      return new google.maps.Marker({ 
       position: new google.maps.LatLng(loc.lat, loc.lng) 
      }); 
     }) 
     .each(function (marker) { 
      marker.setMap(map); 
     }) 
     .value(); 

    var mc = new MarkerClusterer(map, markers); 

    var directionsService = new google.maps.DirectionsService(), 
     directionsDisplay = new google.maps.DirectionsRenderer(), 
     displayRoute  = function() { 
      directionsService.route({ 
       origin: 'Akin Ogunlewe street VI',//6.512596400000001+','+3.3541297 Pass this in place of the address 
       destination: 'Falolu road Surulere',//'Falolu road Surulere',//6.4261139+','+3.4363691 Pass this in place of the address 
       travelMode : google.maps.TravelMode.DRIVING 
      }, function (response, status) { 
       if (status === google.maps.DirectionsStatus.OK) { 
        directionsDisplay.setDirections(response); 
       } else { 
        console.log('Directions request failed due to ' + status); 
       } 
      }); 
     }; 

directionsDisplay.setMap(map); 
displayRoute(); 

有沒有一種方法,我可以通過在OriginLatitudeLongitudeDestination相反的物理地址(字符串)? 我試過了,但沒有奏效。

我需要通過數字(浮點)值,如:6.512596400000001,3.3541297而不是Falolu road Surulere

任何幫助表示讚賞感謝

+0

Waoh!真的不知道什麼是倒票。你寧願忽視或評論,也不願意投反對票。 :) – Michel

回答

2

documentation for the v3 API說,一個google.maps.LatLng或字符串。對於地理位置,請創建並傳入google.maps.LatLng;對於地址,傳入一個字符串。

來源:LatLng |字符串| google.maps.Place, 目的地:LatLng |字符串| google.maps.Place,

而在reference

目的地|類型:地方|目的地的位置。這可以指定爲要進行地理編碼的字符串,也可以指定爲LatLng或Place。需要。

產地|地點|原產地。這可以指定爲要進行地理編碼的字符串,也可以指定爲LatLng或Place。需要。

proof of concept fiddle

代碼片段:

var map; 
 

 
function initialize() { 
 
    var locations = [{ 
 
    lat: 6.4261139, 
 
    lng: 3.4363691 
 
    }]; 
 

 
    var mapContainer = $('.map-container'), 
 
    map = new google.maps.Map(mapContainer[0], { 
 
     center: locations[0], 
 
     zoom: 10 
 
    }); 
 

 
    var directionsService = new google.maps.DirectionsService(), 
 
    directionsDisplay = new google.maps.DirectionsRenderer(), 
 
    displayRoute = function() { 
 
     directionsService.route({ 
 
     origin: new google.maps.LatLng(6.512596400000001, 3.3541297), // Pass this in place of the address 'Akin Ogunlewe street VI' 
 
     destination: new google.maps.LatLng(6.4261139, 3.4363691), // Pass this in place of the address 'Falolu road Surulere' 
 
     travelMode: google.maps.TravelMode.DRIVING 
 
     }, function(response, status) { 
 
     if (status === google.maps.DirectionsStatus.OK) { 
 
      directionsDisplay.setDirections(response); 
 
     } else { 
 
      console.log('Directions request failed due to ' + status); 
 
     } 
 
     }); 
 
    }; 
 

 
    directionsDisplay.setMap(map); 
 
    displayRoute(); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas" class="map-container"></div>

相關問題