Google maps 如何获得一个城市';谷歌地图API上只有城市名称的s Lat、Alt

Google maps 如何获得一个城市';谷歌地图API上只有城市名称的s Lat、Alt,google-maps,javascript,Google Maps,Javascript,我试图通过一个城市的名字来定位它的经纬度 我试图只使用Gmaps API,避免使用谷歌为地理定位提供的Web服务。搜索后,你可以通过place.Point.coordinates 例如: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.or

我试图通过一个城市的名字来定位它的经纬度


我试图只使用Gmaps API,避免使用谷歌为地理定位提供的Web服务。

搜索后,你可以通过
place.Point.coordinates

例如:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Google Maps API Sample</title>
        <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"></script>
        <script type="text/javascript">

        var map;
        var geocoder;

        function initialize() {
         map = new GMap2(document.getElementById("map_canvas"));
         map.setCenter(new GLatLng(34, 0), 1);
         geocoder = new GClientGeocoder();
        }

        // addAddressToMap() is called when the geocoder returns an
        // answer.  It adds a marker to the map with an open info window
        // showing the nicely formatted version of the address and the country code.
        function addAddressToMap(response) {
         map.clearOverlays();
         if (!response || response.Status.code != 200) {
           alert("Sorry, we were unable to geocode that address");
         } else {
           place = response.Placemark[0];
           point = new GLatLng(place.Point.coordinates[1],
                               place.Point.coordinates[0]);
           marker = new GMarker(point);
           map.addOverlay(marker);
  -->      // here you get the coordinates of the founded point
           marker.openInfoWindowHtml('<b>Coordinates:</b> ' + place.Point.coordinates);
         }
        }

        // showLocation() is called when you click on the Search button
        // in the form.  It geocodes the address entered into the form
        // and adds a marker to the map at that location.
        function showLocation() {
         var address = document.forms[0].q.value;
         geocoder.getLocations(address, addAddressToMap);
        }

        // findLocation() is used to enter the sample addresses into the form.
        function findLocation(address) {
         document.forms[0].q.value = address;
         showLocation();
        }

        </script>
      </head>
      <body onload="initialize()" onunload="GUnload()" style="font-family: Arial;border: 0 none;">
        <!-- Creates a simple input box where you can enter an address
             and a Search button that submits the form. //-->
        <form action="#" onsubmit="showLocation(); return false;">
          <p>
            <b>Search cities:</b>
            <input type="text" name="q" value="" class="address_input" size="40" />
            <input type="submit" name="find" value="Search" />

          </p>
        </form>
        <div id="map_canvas" style="width: 500px; height: 300px"></div>
      </body>
    </html>​

谷歌地图API示例
var映射;
var地理编码器;
函数初始化(){
map=newgmap2(document.getElementById(“map_canvas”);
地图设置中心(新格拉特林(34,0),1);
geocoder=新的GClientGeocoder();
}
//当地理编码器返回
//回答。它通过一个打开的信息窗口向地图添加一个标记
//显示地址和国家代码的格式良好的版本。
函数addAddressToMap(响应){
map.clearOverlays();
如果(!response | | response.Status.code!=200){
警报(“抱歉,我们无法对该地址进行地理编码”);
}否则{
地点=响应。地点标记[0];
点=新玻璃(位置点坐标[1],
地点点坐标[0];
标记器=新的GMarker(点);
添加覆盖图(标记);
-->//这里你可以得到建立点的坐标
marker.openInfoWindowHtml('Coordinates:'+place.Point.Coordinates);
}
}
//单击搜索按钮时会调用showLocation()
//在形式上。它对表单中输入的地址进行地理编码
//并在该位置向地图添加标记。
函数showLocation(){
var address=document.forms[0].q.value;
地理编码器。获取位置(地址,addAddressToMap);
}
//findLocation()用于在表单中输入示例地址。
函数查找位置(地址){
document.forms[0].q.value=地址;
showLocation();
}

搜索城市:


真的只有城市吗?这将是一个问题,因为一个给定的城市名称可能代表许多不同的位置。城市名称没有唯一性。