javascript - 谷歌地图 API 函数 getlatlong

标签 javascript php arrays google-maps-api-3 geocode

我有这段代码,问题是 $address_array 只提取数组中的最后一个位置。

我该如何解决?

HTML

<div id="map"></div>

JQUERY

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>  

PHP

<?php
function getlatlong($address){
        $url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=true';
        $json = @file_get_contents($url);
        $data = json_decode($json);
        if ($data->status == "OK")
             return $data;
        else
             return false;
}
$address_array = array('Germany', 'Poland', 'Italy');
foreach ($address_array as $address) {
     $data = getlatlong($address);
     $location = $data->results[0]->geometry->location;
}
?>

JS创建 map

<script>
    function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: {lat: -28.024, lng: 140.887}
        });
    }
</script>
<script defer src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap"></script>

最佳答案

固定

This code wrote one guy and it works great.

代码返回 latlng 位置。

$address_array = array("Germany", "Poland", "Italy","Italy","Italy");
    $encoded_adress_array = [];

    foreach ($address_array as $address) {
        array_push($encoded_adress_array, getlatlong($address)->results[0]->geometry->location);
    }

关于javascript - 谷歌地图 API 函数 getlatlong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558072/

相关文章:

arrays - vba 数组操作硬编码语法

arrays - 如何从 postgres 中的 json 数组中提取特定元素?

javascript - 用于验证的 ASP.NET 客户端 API

javascript - 根据最大后续零删除逗号后的零

java - 发送 arraylist 值并在 php 中获取该数组值

php - 数组 : How to put value in array index?

php - 理解 mvc : difference between a library class and a model class

javascript - 多个玩家 - YouTube api

javascript - 多个 node-mongodb-native 连接

python - 使用 numpy 的 python 中的多线性映射