Created
May 4, 2012 08:32
-
-
Save larryrubin/2593322 to your computer and use it in GitHub Desktop.
PhoneGap Reverse Geolocation Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" content="width=device-width;height=device-height; user-scalable=no" /> | |
<meta http-equiv="Content-type" content="text/html;charset=utf-8"> | |
<title>PhoneGap Reverse Geolocation Lookup</title> | |
<script type="text/javascript" src="cordova-1.7.0.js"></script> | |
<script type="text/javascript" src="jquery-1.7.2.min.js"></script> | |
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script> | |
<script type="text/javascript"> | |
var geocoder; | |
$(document).ready(function() { | |
geocoder = new google.maps.Geocoder(); | |
$('#button-get-reverse-lookup').click(function(){ | |
navigator.geolocation.getCurrentPosition(onGetCurrentPositionSuccess, onGetCurrentPositionError); | |
}); | |
}); | |
var onGetCurrentPositionSuccess = function(position) { | |
console.log("lat: " + position.coords.latitude); | |
console.log("long: " + position.coords.longitude); | |
var lat = parseFloat(position.coords.latitude); | |
var lng = parseFloat(position.coords.longitude); | |
// paris, france - uncomment to test | |
//var lat = parseFloat(48.850258); | |
//var lng = parseFloat(2.351074); | |
// tiburon, california | |
//var lat = parseFloat(37.872685); | |
//var lng = parseFloat(-122.45224); | |
var latlng = new google.maps.LatLng(lat, lng); | |
geocoder.geocode({'latLng': latlng}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
if (results[0]) { | |
var arrAddress = results[0].address_components; | |
// iterate through address_component array | |
$.each(arrAddress, function (i, address_component) { | |
if (address_component.types[0] == "locality") { | |
console.log(address_component.long_name); // city | |
alert(address_component.long_name); | |
return false; // break | |
} | |
}); | |
} else { | |
alert("No results found"); | |
} | |
} else { | |
alert("Geocoder failed due to: " + status); | |
} | |
}); | |
} | |
var onGetCurrentPositionError = function(error) { | |
console.log("Couldn't get geo coords from device"); | |
} | |
</script> | |
</head> | |
<body> | |
<h1>Reverse Geolocation Lookup</h1> | |
<button id="button-get-reverse-lookup">Get Reverse Lookup</button> | |
</body> | |
</html> |
Hi,
Thanks for the great information. I have a doubt though, The code works perfectly wheni i use it with 3g connectivity. But if i try to access it using wi-fi, the code fails. Any inputs why that might be. Am using cordova 2.5.0 and the app runs on android 3.0
This example with google maps api works perfectly fine for me, but I'm wondering if this could be done on native side. Aren't iOS/Android providing some analogs of geocoder?
Hi I Just Copy Your Code And Change the Phonegap File Location . It show me only Black Window. I'm Using Phonegap 2.9.0 version in Windows Phone7 application . Any Idea Plz tell Me Thank You
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Larry
Thanks for this. I'm having a problem though. I'm using cordova-2.2.0 and when I run I'm getting the message
"ReferenceError: Can't find variable: google at file:///android_asset/www/index.html:15"
Did you run into this? Do you have any suggestions about how to get around it?
Mark