Reputation: 467
I have implemented this using php, but I wanted to change it to use JQuery on the client's side.
What I am trying to do is get the data from the JSON file recieved from Googlemaps and use it to save the latitude and longitude to my database, however I have been unable to even get the data into variables.
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#test").append('STUFF HERE');
var geocodeURL = 'http://maps.google.com/maps/api/geocode/json?address=92408&sensor=false';
$.getJSON(geocodeURL, function(data){
var latitude = data.results.geometry.location.lat;
var longitude = data.results.geometry.location.lng;
alert(longitude);
$("#test").append('<p>'+latitude +'</p><p>'+longitude+'</p>');
});
});
</script>
<div id="test" ></div>
When I paste the GeocodeURL into the browser I get the following return:
{
"results" : [
{
"address_components" : [
{
"long_name" : "92408",
"short_name" : "92408",
"types" : [ "postal_code" ]
},
{
"long_name" : "San Bernardino",
"short_name" : "San Bernardino",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Bernardino",
"short_name" : "San Bernardino",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "San Bernardino, CA 92408, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 34.10665490,
"lng" : -117.208780
},
"southwest" : {
"lat" : 34.0501660,
"lng" : -117.3071210
}
},
"location" : {
"lat" : 34.08685170,
"lng" : -117.26173290
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 34.10665490,
"lng" : -117.208780
},
"southwest" : {
"lat" : 34.0501660,
"lng" : -117.3071210
}
}
},
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
After a day or two or researching and tweaking I haven't been able to find out what's wrong, and I'm hoping that this is an easy question for someone who's already been there and done that, cause I've been stuck..
------------------------------UPDATE-------------------------------------
So as far as I can tell, there's no way to do Google Map's JSON with the above method, and instead you must use the following code:
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var geocoder = new google.maps.Geocoder();
var address= '92408';
geocoder.geocode({ 'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
var TEST = results[0].geometry.location;
var longitude = results[0].geometry.location.lng;
alert(TEST);
alert(longitude);
}
else
{
result = "Unable to find address: " + status;
}
});
});
</script>
<div id="test" ></div>
Now the problem is getting the actual latitude and longitude from the .location portion of the returned JSON file. When the ' alert(TEST); ' is run, the following is output:
(34.0868517, -117.26173289999997)
This is the correct long/lat pair, but when I try to put the individual longitude or latitude I get the following output: (from alert(longitude))
function () {
return this[a];
}
Any ideas anyone?
-----------------------------------------Solved------------------------------------
Ok, it was a simple solution.. Change :
var longitude = results[0].geometry.location.lng;
to:
var longitude = results[0].geometry.location.lng();
Upvotes: 1
Views: 3029
Reputation: 3415
Also, I found this thread which might help: using jquery.getJson with Google's GeoCoding HTTP Service Has to do with not providing the call back function
Upvotes: 1