使用Google地图中的地理编码API搜索位置

时间:2013-01-16 05:23:42

标签: google-maps google-maps-api-3 geolocation geocoding

我想使用google maps API实现从Google地图网址"http://maps.google.co.in/maps?q=canara+bank&hl=en&sll=12.953997,77.63094&sspn=1.069318,1.234589&hq=canara+bank&t=m&z=10"呈现的完全相同的结果。

我尝试过使用geoCode API,但有些人无法获得类似的结果。

<!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="https://maps.googleapis.com/maps/api/js?key=AIzaSyDjW1WmSDPUaziJY6DtwMtRLhsGrzy7YLA&sensor=false">
</script>
<script type="text/javascript">

function getLocation()
{
if (navigator.geolocation)
{
    navigator.geolocation.getCurrentPosition(populateData,showError);
}
else{alert("Geolocation is not supported by this browser.");}
}

function showError(error)
  {
  switch(error.code) 
    {
    case error.PERMISSION_DENIED:
     alert("User denied the request for Geolocation.");
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Location information is unavailable.");
      break;
    case error.TIMEOUT:
      alert("The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error occurred.");
      break;
    }
  }

function populateData(position)
{

   var geocoder = new google.maps.Geocoder();
   var address = "canara bank";

    geocoder.geocode( {
                        'address': "canara bank"                        
                    }, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
        var latitude = results[0].geometry.location.lat() ;
        var longitude = results[0].geometry.location.lng();

       var mapOptions = {
                     //display the map center from the user location
                     center: new google.maps.LatLng(position.coords.latitude,position.coords.longitude),
                     zoom: 9,
                     zoomControl:true,
                     zoomControlOptions: {
                     style:google.maps.ZoomControlStyle.SMALL
                     },
             mapTypeId: google.maps.MapTypeId.ROADMAP
            };
           var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);


     var marker = new google.maps.Marker({    
             position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()),    
             map: map    
            });

//for placing the markers
    var markers;
    var i;      
    for(i=0;i<results.length;i++){
        markers = new google.maps.Marker({    
            position: new google.maps.LatLng(results[i].geometry.location.lat(), results[i].geometry.location.lng()),    
            map: map,
           });
     }

     var circleOptions = new google.maps.Circle({
                  center: new google.maps.LatLng(latitude, longitude),
                  radius: 2000,
                  strokeColor: "#FF0000",
                  strokeOpacity: 0.5,
                  strokeWeight: 2,
                  fillColor: "#FF0000",
                  fillOpacity: 0.35,
                  map: map
                });       

    }
} );

}

</script>

</head>
<body onload="getLocation()">

<div id="map_canvas" style="width: 100%; height: 50%"></div>
<form name="form_simple" action=" " method="get">
    <center>
        <label id="d1"> map test</label>
    </center>
</form>

我还想知道在使用地理编码API时是否有 sll (谷歌地图中的参数指定lat和lng)替代方案。

提前致谢。

1 个答案:

答案 0 :(得分:5)

“Canara Bank”不是地址。 geocoding API将地址转换为坐标(来自文档):

  

地理编码是将地址(例如“1600 Amphitheatre Parkway,Mountain View,CA”)转换为   地理坐标(如纬度37.423021和经度-122.083739)

你想要Places API

  

Google Places API是一项服务,可返回有关此API中定义的商家信息   场所,地理位置或重要的兴趣点

Example