我有一个表单,我想添加一个“选择位置”选项。
我该怎么做,以及如何将图钉放置为所选位置?
最佳答案
您可能需要考虑使用 Google Maps API , 作为 davek already suggested .
以下示例可以帮助您入门。您需要做的就是将 JavaScript 变量 userLocation
更改为您的用户从您提到的下拉字段中选择的位置。
<!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 userLocation = 'London, UK';
if (GBrowserIsCompatible()) {
var geocoder = new GClientGeocoder();
geocoder.getLocations(userLocation, 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));
map.addOverlay(new GMarker(bounds.getCenter()));
}
});
}
</script>
</body>
</html>
上面的示例将呈现如下所示的 map :
如果 Google Client-side Geocoder map 将不会显示无法从地址中检索坐标。
关于javascript - 如何在我的网站上添加 Google map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2624833/