Find the latitude and longitude of address using Geocoding API example

In my recent project I faced the task of doing translation of property address in form we usually use it into global coordinates, that can be plotted on the map. Using Google API services this task is quite easy. You can check example and source code below

Geocoding API javascript example

Enter any address below to locate it on the map

Using JavaScript

Example above is using JavaScript version of Geocoding API, so it is possible to call it through the browser. Source code is below:

document.write('<script src="" type="text/javascript"></script>');
document.write('<div style="padding-bottom:10px;"><input type="text" name="address" value="O. J. Brochs g 16a, Bergen" style="width:400px;border:1px solid black"><input type="button" name="search" value="Geocode"></div>');
document.write('<div id="coords"></div>');
document.write('<div id="gmap" style="width:570px; height:500px;"></div>');
jQuery(document).ready(function() {
	// Load google map
	var map = new google.maps.Map( document.getElementById("gmap"),  {
		center: new google.maps.LatLng(0,0),
		zoom: 3,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		panControl: false,
		streetViewControl: false,
		mapTypeControl: false
	jQuery('input[name=search]').click(function() {
		var geocoder = new google.maps.Geocoder(); 
				address : jQuery('input[name=address]').val(), 
				region: 'no' 
		    function(results, status) {
		    	if (status.toLowerCase() == 'ok') {
					// Get center
					var coords = new google.maps.LatLng(
					jQuery('#coords').html('Latitute: ' + + '    Longitude: ' + coords.lng() );
					// Set marker also
					marker = new google.maps.Marker({
						position: coords, 
						map: map, 
						title: jQuery('input[name=address]').val(),

Download geocoding source code here

Using HTTP

Another way to use Geocoding API is though HTTP GET request. This way is more suitable to beused in PHP scripts. All is needed is to make request to the URl below, substituting the real address.,+Mountain+View,+CA&sensor=true_or_false

Response is JSON encoded object, you can see more sample at Google Geocoding API help page

Did you find this post useful? Support the the author ($10)
My Google Profile+


  1. I had search map like you in my site but my Question is how i can show search location co-ordinates in textfield …

Leave a Reply

Your email address will not be published.