【问题标题】:Assign google places details into php variable将 google 地方详细信息分配到 php 变量中
【发布时间】:2017-11-12 18:34:41
【问题描述】:

我正在使用 google places api 来获取一些业务详细信息。我查看了谷歌并找到了在谷歌地图上显示详细信息的方法。我还想要实现的是将详细信息分配给 php 变量。下面的代码在地图上显示了详细信息;

  <script>
      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -9999.9999, lng: 99999.99999},
          zoom: 1
        });

        var infowindow = new google.maps.InfoWindow();
        var service = new google.maps.places.PlacesService(map);

        service.getDetails({
          placeId: 'XXXXXXXXXX'
        }, function(place, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
              map: map,
              position: place.geometry.location
            });

            google.maps.event.addListener(marker, 'click', function() {
              infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
                'Place ID: ' + place.place_id + '<br>' +
                'Telephone: ' + place.formatted_phone_number + '<br>' +
                'Website: ' + place.website + '<br>' +
                place.formatted_address + '</div>');
              infowindow.open(map, this);
            });
          }
        });
      }
    </script>


<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXXXXXX&libraries=places&callback=initMap">
</script>

我希望能够实现的就像下面的代码一样;

 <script>
    var a="Hello";
</script>
<?php 
    echo $variable = "<script>document.write(a)</script>"; 
?>

总而言之,我希望将 google 位置参数分配给 php avriables,以便我可以以任何我想要的方式使用它们。

提前致谢

【问题讨论】:

    标签: php google-places-api


    【解决方案1】:

    四处搜索后,我以不同的方式找到了答案。对于在某些时候可能需要它的任何人/这是下面的代码

    $url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=PLACE_ID&key=API_KEY';
    $json = file_get_contents($url);
    $jsonContent = json_decode($json, TRUE);
    $jsonContent['result']['name'];
    $jsonContent['result']['formatted_phone_number'];
    $jsonContent['result']['formatted_address'];
    $jsonContent['result']['website'];
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2016-10-02
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多