从流星中的地理位置返回邮政编码
问题描述:
<template name="home">
<a href="#" id="showMyLocation">Where Am I</a>
<div id="location">Postal Address</div>
<div id="zipcode">Zip Code</div>
</template>
我想要使用流星中的地理位置检索用户的当前地址和邮政编码。任何帮助或建议将不胜感激。从流星中的地理位置返回邮政编码
这是JavaScript
Meteor.startup(function() {
GoogleMaps.load();
});
Template.home.events({
'click #showMyLocation': function(event){
event.preventDefault();
$(this).html('Determining address...');
navigator.geolocation.getCurrentPosition(function (position){
var geocoder = new google.maps.Geocoder();
var latLng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
geocoder.geocode({
'latLng': latLng},
function (results, status){
for (var i = 0; i < results[0].address_components.length; i++) {
var address = results[0].address_components[i];
if (address.types[0] == "postal_code"){
$('#zipcode').html(address.long_name);
$('#location').html(results[0].formatted_address);
$('#showMyLocation').hide();
}
}
});
});
return false;
}
});
答
你想要做什么叫做Reverse Geocoding。如何使用谷歌地图来做到这一点JavaScript API https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
你也可以使用Geonames API来做到这一点。