1

我想 "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"使用谷歌地图 API 获得从谷歌地图 url 呈现的完全相同的结果。

我尝试过使用 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 的参数)替代方案。

提前致谢。

4

1 回答 1

5

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

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

您想要Places API

Google Places API 是一项服务,可返回有关 Places 的信息——在此 API 中定义为场所、地理位置或著名的兴趣点

例子

于 2013-01-16T06:51:34.673 回答