admin管理员组文章数量:1394228
I'm looking to geoencode an address and convert it to GPS Coordinates. Basically,
var address;
// Google/Yahoo/whatever program to convert address to GPS coordinates
var output=xx.xxxxxxx,xx.xxxxxxx
I've researched Google and Yahoo APIs, but can't seem to get their codes to work in my Google Gadget. Any suggestions?
Thanks in advance!
I'm looking to geoencode an address and convert it to GPS Coordinates. Basically,
var address;
// Google/Yahoo/whatever program to convert address to GPS coordinates
var output=xx.xxxxxxx,xx.xxxxxxx
I've researched Google and Yahoo APIs, but can't seem to get their codes to work in my Google Gadget. Any suggestions?
Thanks in advance!
Share Improve this question asked Jul 3, 2012 at 20:04 zdebruinezdebruine 3,8176 gold badges33 silver badges52 bronze badges 1- 2 Both Google API and Yahoo API do this, so my advice is to post your code that is not working. – Eric J. Commented Jul 3, 2012 at 20:06
3 Answers
Reset to default 4Here's what I did for my address-to-gps-coords needs:
function get_coords(address)
{
var gc = new google.maps.Geocoder(),
opts = { 'address' : address };
gc.geocode(opts, function (results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
var loc = results[0].geometry.location,
lat = loc.$a,
long = loc.ab;
// Success. Do stuff here.
}
else
{
// Ruh roh. Output error stuff here
}
});
}
Then you call it like get_coords('1600 Pennsylvania Avenue NW Washington, DC 20500')
and it'll return the latitude and longitude.
You'll need to supply your own Google Maps API key to make it work, of course.
Hope this helps!
I did this and worked perfect:
<script type="text/javascript">
var dir1 = "5th ave, new york";
var google_url = "http://maps.googleapis./maps/api/geocode/json?address=";
var sensor = "&sensor=false";
var resultado;
function myFunction(){
$.getJSON(
google_url+dir1+sensor,
function(result){
$( "body" ).append( "Latitude: " + JSON.stringify(result.results[0].geometry.bounds.northeast.lat) )
$( "body" ).append( "Longitude: " + JSON.stringify(result.results[0].geometry.bounds.northeast.lng) )
});
}
I help to maintain one API called LiveAddress which does this. Much easier to use than Google/Yahoo's APIs, and without the restrictive Terms of Service which prohibit requests en masse, storing the results, or using the data without showing a map or by automation.
Here's a plete example, using this sample wrapper function:
LiveAddress.init(123456789); // your API key here
LiveAddress.geocode("address goes here", function(geo) {
// You can also pass in an array of addresses,
// the ID of a DOM element containing the address,
// or an array of IDs
console.log(geo);
});
Individual coordinates are found in geo.lat
and geo.lon
, with the bined string "lat, lon" format in geo.coords
. You can also obtain the precision of the data with geo.precision
.
本文标签: javascriptGeoencode Address to GPS CoordinatesStack Overflow
版权声明:本文标题:javascript - Geoencode Address to GPS Coordinates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744673801a2618987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论