如何从async php db search向geocoder发送地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从async php db search向geocoder发送地址相关的知识,希望对你有一定的参考价值。

我正在尝试让我的谷歌地图应用程序等待来自使用php的数据库调用的ajax调用的答案。我知道我需要使用回调但我不确定如何在这种情况下实现它们,因为地理编码器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

答案

将ajax请求放在initMap中,当地址可用时,在其回调函数中调用地址解析器:

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)

以上是关于如何从async php db search向geocoder发送地址的主要内容,如果未能解决你的问题,请参考以下文章

php [php:array_search + array_column]来自db的saerch数组返回array_search + array_column。 #PHP

Elasticsearch:Async search API

Elasticsearch:Async search API

使用 slug url 从 mysql db 获取文章

从 Nodejs 中的 mySQL 获取数据时如何从 Async/await 函数获取返回值

Swift - 我如何等待 dispatch_async 完成?