PHP 变量中的地址,没有 Javascript 的谷歌地图嵌入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12123164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:52:52  来源:igfitidea点击:

Address in PHP variable, Google Maps embedding without Javascript?

phpgoogle-mapsgeocoding

提问by user1555076

I consider myself to be an average PHP coder, but I can barely edit the few bits of JavaScript code that I need on my websites. I have an address in a PHP variable and I would like a simple map to show up on my webpage with that address. I've done some research on the internet and I find a lot op geocoding, converting an address to a location the Google API understands, but I'm completely lost. Is there no simple piece of code to in which I can embed my variables?

我认为自己是一个普通的 PHP 编码员,但我几乎无法编辑我网站上需要的几段 JavaScript 代码。我在 PHP 变量中有一个地址,我希望一个简单的地图显示在我的网页上,并带有该地址。我在互联网上做了一些研究,我发现了很多操作地理编码,将地址转换为 Google API 理解的位置,但我完全迷失了。没有简单的代码可以嵌入我的变量吗?

回答by Napolux

You can use an iframe and pass the address as an URL parameter.

您可以使用 iframe 并将地址作为 URL 参数传递。

<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $yourAddress; ?>&output=embed"></iframe>

回答by Nasreddine

You'll have to use the Geocoder API.

您必须使用Geocoder API

Here you go ( original can be found here) :

给你(原文可以在这里找到):

<head>
...
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();

    var mapOptions = {
      zoom: 12, //Change the Zoom level to suit your needs
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    //map_canvas is just a <div> on the page with the id="map_canvas"
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    //Your Variable Containing The Address
    var address = "<?php echo $AddressVariable; ?>"; 
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        //places a marker on every location
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });

  }

</script>
...
</head>

Add this to the opening bodytag of your page to initialize the map:

将此添加到body页面的开始标记以初始化地图:

<body onload="initialize()">

And here'sthe documentation for the Geocoderclass.

这里是对的文档Geocoder类。

回答by Vineesh Kalarickal

These links are use full to you..not the exact link but its use full :) tc..

这些链接对你来说是完整的......不是确切的链接,而是完整的使用:) tc..

1)remove-address-bubble-from-google-maps-iframe

1)删除地址气泡从谷歌地图 iframe

2) google maps

2)谷歌地图

回答by Vishnu Bhadoriya

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<?php 
 $address = str_replace(" ", "+",$address_variable);
?>
<iframe style="width:100%;height:50%;" frameborder="0" id="cusmap" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $address; ?>&output=embed"></iframe>