2010-04-12 90 views
3

我有一个表单,我想添加一个“选择位置”选项。如何在我的网站上添加Google地图?

我该如何做到这一点,以及如何将销钉作为选定的位置?

+3

我们可以看到一些代码吗?或更多的背景?或两者? – 2010-04-12 19:38:49

回答

5

你可能要考虑使用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&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 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> 

以上例子将呈现图像下面的一个:

Render google map in based on selected location

的地图将不会显示,如果Google Client-side Geocoder不能retreive根据输入的地址的坐标。

2

查看Google Maps API。这里有很多信息和几个例子:不了解更多关于你的环境/需求的信息,它很难更具体。

相关问题