You are currently viewing Getting geo-location of any city using Google API

In this article, we will discuss about getting geo-location of any city. For getting geo-location of any city in the form of longitude and latitude, we use Google API. For this, you have to generate a Google API key which I have already discussed in my last article Getting Geo-Location of Current Location. You can use the single for getting the current location as well as for getting the location of any city. Following is the source code and output.


Source Code (jQuery and HTML):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head runat="server">

<title></title>  http://Scripts/jquery-1.4.1-vsdoc.js



function initialize() {

var address = (document.getElementById('my-address'));

var autocomplete = new google.maps.places.Autocomplete(address);


google.maps.event.addListener(autocomplete, 'place_changed', function () {

var place = autocomplete.getPlace();

if (!place.geometry) {



var address = '';

if (place.address_components) {

address = [

(place.address_components[0] && place.address_components[0].short_name || ''),

(place.address_components[1] && place.address_components[1].short_name || ''),

(place.address_components[2] && place.address_components[2].short_name || '')

].join(' ');




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

function codeAddress() {

geocoder = new google.maps.Geocoder();

var address = document.getElementById("my-address").value;

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

if (status == google.maps.GeocoderStatus.OK) {

success(results[0].geometry.location.lng(), results[0]


else {

alert("Geocode was not successful for the following reason: " + status);




function success(long, lat) {

var myLatlng = new google.maps.LatLng(lat, long);

var myOptions = {

center: myLatlng,

zoom: 12,

mapTypeId: google.maps.MapTypeId.ROADMAP


var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var marker = new google.maps.Marker({

position: myLatlng,

title: "lat: " + lat + " long: " + long



var infowindow = new google.maps.InfoWindow({ content: "User Address
Latitude:" + lat + "
Longitude:" + long + "" });, marker);






<input type="text" id="my-address" />

<input id="getCords" type="button" value="Find Location" onclick="javascript:codeAddress()" />

<div id="map_canvas" style="width: 500px; height: 400px"></div>






First Enter the City name and click on Find Location as shown below.


Dinesh Kumar Bansal

Dinesh Kumar Bansal is an Indian Software Developer, who is working on ASP.Net, MS-SQL, SAP-ABAP technologies from last one year. His Basic Principle of developing software is, “You have to clear about, what do you want to do, how can it be done”. He always says that development can be done with a cool & fresh mind.

Leave a Reply