Internet

Google map javascript client to find the geographic co-ordinates from Address or post code

October 17, 2014 by Sarath DR in Internet, Tips with 0 Comments

Its very simple with Google api to find geographic co-ordinates of a place. I just wrote a sample code, you can add/improve it if you want.

You have to create a new api project on Google apps. Click the below link and create a new project.

https://console.developers.google.com/

Screen Shot 2014 10 17 at 16.00.37 300x177 Google map javascript client  to find the geographic co ordinates  from  Address or post code

Click on Apis and enable Geocode api service.

Screen Shot 2014 10 17 at 16.17.37 300x39 Google map javascript client  to find the geographic co ordinates  from  Address or post code

Click on Credentials to create the Api Key. (Server api Key)

Screen Shot 2014 10 17 at 16.20.34 300x163 Google map javascript client  to find the geographic co ordinates  from  Address or post code

Use the Api key in the below code to access the Google maps api. Jquery library is required to execute the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Geocoder(apiKey) {
 
    this.apiKey = apiKey;
    this.url = "https://maps.google.com/maps/api/geocode/json";
    this.address = "";
    _sendData = function (urlMap, callBack) {      
 
      $.ajax({
            url: urlMap,
            type: 'GET',
            dataType: 'json',
            success: function (data) {                                          
              if(data.status == "OK"){                
                 callBack( data.results[0].geometry.location);  
 
              }                             
            }
        });
    };
 
 
}
 
Geocoder.prototype.getLocation = function (postCode, country, callBack ) {
 
    urlMap = this.url + "?key=" + this.apiKey + "&address=" + postCode + " " + country;
    _sendData(urlMap, callBack);
}
 
var loc = new Geocoder("apikey");
 
loc.getLocation("your-postcode-or-address", "united kingdom", function( geometry ){
    console.log(geometry);
    alert(geometry.lat);
});

Tagged , ,

Share this to

Related Posts

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close