views:

209

answers:

1

Dear all,

I would like to get client location dynamically through google maps api.

Below are 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 error in the code? Please advice. Thank you.

+1  A: 

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);
};
kender
hi kender, sorry i have left out the closing bracket im the coding part in my question. But it exist in the apps. There's no syntax error in while the app run. But couldn't get client location. It returns null value.
benmsia
well, the exact code I pased into a .html file, inside a <script></script> tag, gave me my (approx.) location... Tested with Firefox and Chrome on Linux
kender