Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to get users country name. So that i can display users country flag in my application. I tried many ways but i'm getting server location instead of client location. Please help me.
Posted
Comments
Kornfeld Eliyahu Peter 8-Dec-14 5:43am    
Unclear. You may share your code with us...
sumanth n 8-Dec-14 5:57am    
<script language="Javascript" src="http://www.codehelper.io/api/ips/?js&callback=mycallback"></script>
<script>
function mycallback(json) {
alert(json.IP);
}
</script>

in the above code i'm getting an issue mycallback is undefined. Please help me
Kornfeld Eliyahu Peter 8-Dec-14 6:00am    
I can't see how that related to your original question, but it is about the order of your code...
First you use mycallback, and then you define it - switch the order...
sumanth n 8-Dec-14 6:08am    
if you open link in that src, you can find a json string about client location. I'm trying to access it using mycallback method. Below link may bring you more clarity

http://www.codehelper.io/community/code-example/javascript-jquery-get-client's-ip-address-location-(country-city)/
mudgilsks 8-Dec-14 6:36am    
try this

<script type="text/javascript" src="http://www.google.com/jsapi?key=YOURKEY"></script>

<script type="text/javascript">

function geoTest() {

if (google.loader.ClientLocation) {

var latitude = google.loader.ClientLocation.latitude;
var longitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var country = google.loader.ClientLocation.address.country;
var country_code = google.loader.ClientLocation.address.country_code;
var region = google.loader.ClientLocation.address.region;

var text = 'Your Location<br /><br />Latitude: ' + latitude + '<br />Longitude: ' + longitude + '<br />City: ' + city + '<br />Country: ' + country + '<br />Country Code: ' + country_code + '<br />Region: ' + region;

} else {

var text = 'Google was not able to detect your location';

}

document.write(text);

}

geoTest();

</script>

more details:
http://www.adammcfarland.com/2009/11/19/simple-ip-geolocation-using-javascript-and-the-google-ajax-search-api/

1 solution

 
Share this answer
 
Comments
sumanth n 8-Dec-14 6:11am    
HI, that's a useful link. Will it works on server side or client side?
mudgilsks 8-Dec-14 6:15am    
server side
sumanth n 8-Dec-14 6:30am    
I need it to run on Client side as per my requirement. Could you suggest me any.
mudgilsks 8-Dec-14 6:41am    
try this

<script type="text/javascript" src="http://www.google.com/jsapi?key=YOURKEY"></script>

<script type="text/javascript">

function geoTest() {

if (google.loader.ClientLocation) {

var latitude = google.loader.ClientLocation.latitude;
var longitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var country = google.loader.ClientLocation.address.country;
var country_code = google.loader.ClientLocation.address.country_code;
var region = google.loader.ClientLocation.address.region;

var text = 'Your Location<br /><br />Latitude: ' + latitude + '<br />Longitude: ' + longitude + '<br />City: ' + city + '<br />Country: ' + country + '<br />Country Code: ' + country_code + '<br />Region: ' + region;

} else {

var text = 'Google was not able to detect your location';

}

document.write(text);

}

geoTest();

</script>

more details:
http://www.adammcfarland.com/2009/11/19/simple-ip-geolocation-using-javascript-and-the-google-ajax-search-api/
sumanth n 8-Dec-14 7:03am    
Ok thanks i found the answer.. Thanks for you help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900