我有一个包含大量页面的 wordpress 站点,每个页面代表一个物理位置。现在对于每个页面,我想根据地址显示一个谷歌地图。我知道我可以通过安装例如 Geo Mashup 插件来做到这一点 http://wordpress.org/extend/plugins/geo-mashup/但这需要(我相信)我手动为每个帖子创建一个基于地址的位置,并向帖子/页面添加一个短代码以生成谷歌地图。对于这个拥有数百个位置的网站来说,这是一项大量的工作。
我希望能够
A: Create an "address-custom-field" for each post programmatically.
B: In a page template use that custom field to render a google map.
A很容易,但是B呢?
最佳答案
您可能需要考虑使用 Google Maps API .
以下示例可以帮助您入门。您需要做的就是将 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&v=2&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>
上面的示例将呈现如下所示的 map :
如果 Google Client-side Geocoder map 将不会显示无法从地址中检索坐标。
关于javascript - 根据地址自定义字段在 wordpress 中呈现谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2297486/