谷歌地图Geocoder如何在新的google.maps.LatLng中返回值?

时间:2016-10-26 10:54:54

标签: javascript google-maps

我有这段代码:

function getLatLong(address) {
        var geocoder = new google.maps.Geocoder();
        var result = "";
        geocoder.geocode( { 'address': address }, function(results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
                 result[lat] = results[0].geometry.location.Pa;
                 result[lng] = results[0].geometry.location.Qa;
             } else {
                 result = "Unable to find address: " + status;
             }
             storeResult(result);
            });
      }





        directionsDisplay = new google.maps.DirectionsRenderer();
        var directionsService = new google.maps.DirectionsService();

        var request = {
          origin: new google.maps.LatLng(getLatLong()[0],getLatLong()[1]), 
          destination: new google.maps.LatLng(59.79530896374892,30.410317182540894), 
          travelMode: google.maps.DirectionsTravelMode.DRIVING /
        };

        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });

        map = new google.maps.Map(document.getElementById("map"));
        directionsDisplay.setMap(map);

我如何返回值origin:new google.maps.LatLng()?问题回调,如何在全局变量中保存值?请帮忙解决它。我没有想法。

1 个答案:

答案 0 :(得分:0)

地理编码器是异步的,你不能从回调函数返回值,你需要在它们可用的时间/地点使用它们(在回调函数中)。

一个选项是在Geocoder的回调函数中进行路线服务调用:

function getLatLong(address) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var request = {
        origin: results[0].geometry.location,
        destination: new google.maps.LatLng(59.79530896374892, 30.410317182540894),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };

      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        } else {
          alert("directions failed:" + status)
        }
      });
    } else {
      result = "Unable to find address: " + status;
    }
  });
}

proof of concept fiddle

代码段

var geocoder;
var map;
var directionsDisplay;
var directionsService;

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  directionsDisplay = new google.maps.DirectionsRenderer();
  directionsService = new google.maps.DirectionsService();
  map = new google.maps.Map(document.getElementById("map"));
  directionsDisplay.setMap(map);
  getLatLong("St. Petersburg, Russia");

}
google.maps.event.addDomListener(window, "load", initialize);

function getLatLong(address) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var request = {
        origin: results[0].geometry.location,
        destination: new google.maps.LatLng(59.79530896374892, 30.410317182540894),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };

      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        } else {
          alert("directions failed:" + status)
        }
      });
    } else {
      result = "Unable to find address: " + status;
    }
  });
}
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>