Vignesh Bala
Vignesh Bala

Reputation: 919

Find location using address in google map

Is it possible to get exact location in google maps using the address and zip-code of the location? If not, suggests me some other way to solve this problem. I searched a lot but I won't find how to do that.

Thanks in advance......

Upvotes: 0

Views: 2481

Answers (2)

Ajay Malhotra
Ajay Malhotra

Reputation: 838

Given below is the code. just add location and click geocode button.

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Geocoding service</title>
<style>
  html, body{
    margin: 0px;
    padding: 0px;
  }
  #panel {
    position: absolute;
    top: 5px;
    left: 50%;
    margin-left: -180px;
    z-index: 5;
    background-color: #fff;
    padding: 5px;
    border: 1px solid #999;
  }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false"></script>
<script>
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 13,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeAddress() {
document.getElementById("dis").setAttribute('style','visibility:visible;');
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
  map.setCenter(results[0].geometry.location);
  var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location
    });
} else {
  alert('Geocode was not successful for the following reason: ' + status);
}
 });
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
<style type="text/css">
.display{visibility:hidden ;width: 700px;height: 400px;position: fixed;top: 0;left: 0;right: 0;bottom: 0;margin: auto;}
</style>
 </head>
 <body>
<div id="panel">
  <input id="address" type="textbox" value="Pathankot">
  <input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div class="display" id="dis">
<div id="map-canvas" style="width:400px;height:400px"></div>
</div>
</body>
</html>

Upvotes: 2

Liem Do
Liem Do

Reputation: 943

You can use Reverse Geocoding (Address Lookup) of google map https://developers.google.com/maps/documentation/javascript/geocoding

Upvotes: 2

Related Questions