jasdmystery
jasdmystery

Reputation: 1752

Search location using geocode API in google maps

I would like to acheive the exact same result rendered from a google maps url "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" using the google maps API.

I've tried using the geoCode API, but some have been unable to achieve a similar result.

<!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>

I would also like to know if there is an sll(parameter in the google maps to specify lat and lng ) alternative while using the geocode API.

Thanks in advance.

Upvotes: 1

Views: 4072

Answers (1)

geocodezip
geocodezip

Reputation: 161324

"Canara Bank" is not an address. The geocoding API converts addresses to coordinates (from the documentation):

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739)

You want the Places API:

The Google Places API is a service that returns information about Places — defined within this API as establishments, geographic locations, or prominent points of interest

Example

Upvotes: 5

Related Questions