Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  182] [ 3]  / answers: 1 / hits: 28728  / 13 Years ago, mon, april 11, 2011, 12:00:00

I'm trying to get lng and lat coordinates of the Google Maps API by the next example http://jsbin.com/inepo3/7/edit. I expect a 'success' popup, but it keeps showing the 'Error' popup.
The google maps-request gives the correct json feedback (checked by firebug).



<script type=text/javascript>
$().ready(function() {

$.fn.getCoordinates=function(address){

$.ajax(
{
type : GET,
url: http://maps.google.com/maps/api/geocode/json,
dataType: jsonp,
data: {
address: address,
sensor: true
},
success: function(data) {
set = data;
alert(set);
},
error : function() {
alert(Error.);
}
});
};

$().getCoordinates(Amsterdam, Netherlands);
});
</script>


Does anyone know how to fix this issue?



Regards,
Guido Lemmens



EDIT



I found a bether solution using the Google Maps Javascript API combined in jQuery:



<script type=text/javascript>
$().ready(function() {
var user1Location = Amsterdam, Netherlands;
var geocoder = new google.maps.Geocoder();
//convert location into longitude and latitude
geocoder.geocode({
address: user1Location
}, function(locResult) {
console.log(locResult);
var lat1 = locResult[0].geometry.location.lat();
var lng1 = locResult[0].geometry.location.lng();
$(#testDiv).html(latitude: + lat1 + <p>longitude: + lng1 + </p>);
});
});
</script>

More From » jquery

 Answers
66

Google Map API V3 makes it harder for external libraries to work with JSONP. Here is a blog post about it.



JSONP and Google Maps API Geocoder Plus A Fix w/ jQuery






An alternative way of getting Geocoding is to use the Google Map V3 API Geocoder Service. Here is an example that i helped a person that was having a similar issue as you to replace his JSONP to use Google Map V3 Geocoder Service. Take a look at this JSFiddle Demo:



This is basically the core. We basically use twitter to get the tweet's address (IE. London, Madrid or Georgia etc) and convert the actual address into LatLng using Google Map's Geocoder Service:



$.getJSON(
url1, function(results) { // get the tweets
var res1 = results.results[0].text;
var user1name = results.results[0].from_user;
var user1Location = results.results[0].location;

// get the first tweet in the response and place it inside the div
$(#last-tweet1).html(res1 + <p>from: + user1name + ( + user1Location + )</p><p>);
//convert location into longitude and latitude
geocoder.geocode({
address: user1Location
}, function(locResult) {
console.log(locResult);
var lat1 = locResult[0].geometry.location.lat();
var lng1 = locResult[0].geometry.location.lng();
$(#testDiv).html(latitude: + lat1 + <p>longitude: + lng1 + </p>);
});
});

[#92807] Saturday, April 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iliana

Total Points: 246
Total Questions: 109
Total Answers: 82

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;