Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于地址自定义字段在wordpress中呈现google地图_Javascript_Wordpress_Google Maps - Fatal编程技术网

Javascript 基于地址自定义字段在wordpress中呈现google地图

Javascript 基于地址自定义字段在wordpress中呈现google地图,javascript,wordpress,google-maps,Javascript,Wordpress,Google Maps,我有一个有大量页面的wordpress站点,每个页面代表一个物理位置。现在,我想为每个页面显示一个基于地址的谷歌地图。我知道我可以通过安装Geo Mashup插件来做到这一点,但这需要(我相信)我手动为每一篇帖子创建一个基于地址的位置,并在帖子/页面中添加一个短代码,从而生成谷歌地图。这是一个有数百个地点的网站大量的工作 我希望能够 A:创建“地址自定义字段” 以编程方式为每一篇文章提供支持。 B:在 页面模板使用该自定义字段 渲染谷歌地图 A很容易,但B</p>< p>您可能想考虑使用. 以

我有一个有大量页面的wordpress站点,每个页面代表一个物理位置。现在,我想为每个页面显示一个基于地址的谷歌地图。我知道我可以通过安装Geo Mashup插件来做到这一点,但这需要(我相信)我手动为每一篇帖子创建一个基于地址的位置,并在帖子/页面中添加一个短代码,从而生成谷歌地图。这是一个有数百个地点的网站大量的工作

我希望能够

A:创建“地址自定义字段” 以编程方式为每一篇文章提供支持。
B:在 页面模板使用该自定义字段 渲染谷歌地图


A很容易,但B</p>< p>您可能想考虑使用. 以下示例可能有助于您入门。您需要做的就是使用页面中位置功能的地址更改JavaScript变量

yourAddress
。“如果A是容易的”,这应该是非常直截了当的

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps API Demo</title> 
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body onunload="GUnload()"> 

    <div id="map_canvas" style="width: 400px; height: 300px"></div> 

    <script type="text/javascript"> 

    var yourAddress = 'London, UK';

    if (GBrowserIsCompatible()) {
       var geocoder = new GClientGeocoder();
       geocoder.getLocations(yourAddress, function (locations) {         
          if (locations.Placemark)
          {
             var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
             var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
             var east  = locations.Placemark[0].ExtendedData.LatLonBox.east;
             var west  = locations.Placemark[0].ExtendedData.LatLonBox.west;

             var bounds = new GLatLngBounds(new GLatLng(south, west), 
                                            new GLatLng(north, east));

             var map = new GMap2(document.getElementById("map_canvas"));

             map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
          }
       });
    }
    </script> 
  </body> 
</html>

谷歌地图API演示
var yourAddress=‘英国伦敦’;
if(GBrowserIsCompatible()){
var geocoder=new GClientGeocoder();
geocoder.getLocations(你的地址,函数(位置){
if(位置、位置标记)
{
var north=locations.Placemark[0]。ExtendedData.LatLonBox.north;
var south=locations.Placemark[0]。ExtendedData.LatLonBox.south;
var east=位置。位置标记[0]。ExtendedData.LatLonBox.east;
var west=位置。Placemark[0]。ExtendedData.LatLonBox.west;
var bounds=新GLatLng bounds(新GLatLng(南部、西部),
新格拉特林(北部、东部);
var map=newgmap2(document.getElementById(“map_canvas”);
setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
}
});
}
上面的示例将渲染如下所示的贴图:


如果无法从地址检索到坐标,地图将不会显示。

这现在已经过时,Google已删除v2版服务

用这样的东西

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {
        var myOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>

html{高度:100%}
正文{高度:100%;边距:0;填充:0}
#地图画布{高度:100%}
函数初始化(){
变量myOptions={
中心:新google.maps.LatLng(-34.397150.644),
缩放:8,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),
肌肽);
}

谢谢,太好了。我注意到我必须将我的GoogleMapsAPI键作为get参数添加到javascript调用中。(key=)@windyjonas:如果使用最新版本的mapsapi(v3),则不需要密钥。在示例中如何使用地址?