Google方向API - 如何传递纬度和经度而不是物理地址

时间:2016-01-25 14:00:19

标签: javascript google-maps google-maps-api-3 geolocation google-geocoder

真的需要一些想法。一直在使用谷歌地图API,我使用地理编码,反向地理编码和地理位置。通过下面这段代码,我可以使用Google地图的Direction 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();

我有没有办法在LatitudeLongitude中传递OriginDestination而不是物理地址(字符串)? 我尝试了但是没有用。

我需要传递数字(float)值,如:6.512596400000001,3.3541297而不是Falolu road Surulere

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

documentation for the v3 API表示google.maps.LatLng或字符串。对于地理位置,请创建并传入google.maps.LatLng;对于地址,传入一个字符串。

  

origin: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>