Minichan

Topic: Need AJAX help

Anonymous A started this discussion 15 years ago #13,456

function showPosition(position) {
//alert('Lat: ' + position.coords.latitude + ', Lon: ' + position.coords.longitude);
var latlng = position.coords.latitude + ',' + position.coords.longitude;
geocoder.getLocations(latlng, function(addresses) {
if(addresses.Status.code != 200) {
alert("fail");
}
else {
address = addresses.Placemark[0];
}

var placename = address.address;

});
$.ajax({
type: "POST",
url: "storelocation.php",
data: "long=" + position.coords.longitude + "&lat=" + position.coords.latitude + "&place=" + placename,
success : function(data){
alert(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
}

if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}



Basically this gets the long/latitude using W3C geolocation API and uses google map reverse geocoding to get an address. But the address doesn't send through the ajax, it just fails completely. There is nothing wrong with the SQL query I got running on a separate page.

If I take

> + "&place=" + placename

out, then it works fine (except doesn't log the place name with the rest of the info.)

Halp plz.

SQUARED :3 !L.....AlN. joined in and replied with this 15 years ago, 15 minutes later[^] [v] #241,234

It's broken.

Anonymous A (OP) replied with this 15 years ago, 2 minutes later, 18 minutes after the original post[^] [v] #241,236

@previous (SQUARED :3 !L.....AlN.)

Thanks, Watson. Anything you suggest to fix it?

ltamake !!onnLC5KY8 joined in and replied with this 15 years ago, 2 minutes later, 21 minutes after the original post[^] [v] #241,237

Uh, are you sure placename resolves to a valid integer? If it's invalid, it won't work.

(Edited 2 minutes later.)

Anonymous A (OP) replied with this 15 years ago, 5 minutes later, 27 minutes after the original post[^] [v] #241,239

@previous (ltamake !!onnLC5KY8)

...fairly. In fact, no, I'm not. Fucktits.

Anonymous D joined in and replied with this 15 years ago, 5 days later, 5 days after the original post[^] [v] #244,444

bump

placename is defined inside the geocoder.getLocations function scope, which is a callback. That means, it gets called when the ajax call completes. Move your $.ajax call right after var placename = address.address; for fun & profit.
:

Please familiarise yourself with the rules and markup syntax before posting.