将谷歌地点详细信息分配到php变量中



我正在使用谷歌地方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>"; 
?>

所以总而言之,我希望谷歌将参数分配给php avriables,这样我就可以以任何我想要的方式使用它们。

提前致谢

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

$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'];

希望对你有帮助

最新更新