谷歌地图地理编码器如何我可以返回值在新的Google .maps. latlng

Google map Geocoder how I can return values in new google.maps.LatLng?

本文关键字:Google latlng maps 返回值 我可以 编码器 谷歌地图      更新时间:2023-09-26

我有这样的代码:

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() ?问题回调,我如何保存值在全局变量?请帮忙修理。我不知道。

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

  • reference:如何从异步调用返回响应?

一个选项,在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;
    }
  });
}

概念验证

代码片段:

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>