如何从异步 php db 搜索向地理编码器发送地址

Posted

技术标签:

【中文标题】如何从异步 php db 搜索向地理编码器发送地址【英文标题】:How to send address to geocoder from async php db search 【发布时间】:2019-06-16 07:24:12 【问题描述】:

我正在尝试让我的谷歌地图应用程序等待来自使用 php 的 db 调用的 ajax 调用的答案。我知道我需要使用回调,但我不确定在这种情况下如何实现它们,因为 geocoder api 也是异步的。

我尝试了 How to pass variables and data from PHP to javascript? 和 Using gettext with Javascript from PHP via XMLHttpRequest 的回答,但我不确定如何编辑它以适应我的情况。

db.php

$mysqli = new mysqli("host", "user", "pass", "db");
(perform query)...
echo json_encode($result_assoc_array);

location.php

<html>
(html boilerplate)...
<body>
<div id="map"></div>
<script>
var map;
var homeAddress;

var oReq = new XMLHttpRequest();
oReq.onload = function() 
    homeAddress = JSON.parse(this.responseText).address; //main field I want is address
;
oReq.open("get", "db.php", true);

oReq.send();

function initMap() 
    map = new google.maps.Map(document.getElementById('map'), 
        center:  lat: -34.397, lng: 150.644 ,
        zoom: 15,
    );

    var bounds = new google.maps.LatLngBounds();
    var geocoder = new google.maps.Geocoder();
    var markers = [];

    geocoder.geocode(
        'address': JSON.stringify(homeAddress)
    , function(results, status) 
        if (status == google.maps.GeocoderStatus.OK) 
            console.log(results);
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker(
                map: map,
                position: results[0].geometry.location
            );
            bounds.extend(marker.position);
            markers.push(marker);
         else 
            alert("Geocode was not successful for the following reason: " + status);
        
    );
 (more logic)
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=myKey&callback=initMap&libraries=geometry" async defer></script>
</body>
</html>

这应该给我一张地图,其标记以来自 db 的地址为中心,但我得到的是Geocode was not successful for the following reason: INVALID_REQUEST,因为homeAddressundefined

【问题讨论】:

【参考方案1】:

将ajax请求放入initMap,地址可用时在其回调函数中调用geocoder:

function initMap() 
    map = new google.maps.Map(document.getElementById('map'), 
        center:  lat: -34.397, lng: 150.644 ,
        zoom: 15,
    );

    var bounds = new google.maps.LatLngBounds();
    var geocoder = new google.maps.Geocoder();
    var markers = [];

    var oReq = new XMLHttpRequest();
    oReq.onload = function() 
      homeAddress = JSON.parse(this.responseText).address; //main field I want is address
      geocoder.geocode(
        'address': JSON.stringify(homeAddress)
      , function(results, status) 
        if (status == google.maps.GeocoderStatus.OK) 
          console.log(results);
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker(
            map: map,
            position: results[0].geometry.location
          );
          bounds.extend(marker.position);
          markers.push(marker);
         else 
          alert("Geocode was not successful for the following reason: " + status);
        
      );
    ;
    oReq.open("get", "db.php", true);

    oReq.send();    
 (more logic)

【讨论】:

以上是关于如何从异步 php db 搜索向地理编码器发送地址的主要内容,如果未能解决你的问题,请参考以下文章

前向地理编码

PHP 地理编码,从地址到坐标lat long

IP地址搜索的地理位置帮助?

使用前向地理编码获取iOS中许多地址的纬度和经度时如何添加延迟?

如何用PHP获取到百度地图坐标或者经纬度

如何使用 PHP 对 MapQuest 地理编码器进行批量地理编码?