2015-07-21 67 views
1
<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; 
} 
}); 

回答

相關問題