davidlee
davidlee

Reputation: 6167

Get client's location through Google Map API

I would like to get the client location dynamically through Google Maps API.

Below is my code:

/*Call external api dynamically*/
var script = document.createElement("script");
  script.src = "http://www.google.com/jsapi?key=" + api_key + "&callback=loadm";
  script.type = "text/javascript";
  document.getElementsByTagName("head")[0].appendChild(script);

/*function */
function loadm() {
 google.load("maps", "3", {other_params:"sensor=false", "callback" : defmap});
}

function defmap() {

 if(google.loader.ClientLocation){
    alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude};
}

I have tried it, but null values are returned. Is there an error in the code?

Upvotes: 0

Views: 2222

Answers (1)

kender
kender

Reputation: 87161

It seems to me you got some syntax error in your alert line of code. Try to close all brackets, this line worked for me:

if (google.loader.ClientLocation) {
    alert(google.loader.ClientLocation.latitude+" "+google.loader.ClientLocation.longitude);
};

Upvotes: 2

Related Questions