我使用的是Google GeoLocation API,这是我的Jade代码:
extends layout
block content
p Hi
script(type='text/javascript').
$(document).ready(function() {
//I'm not doing anything else, so just leave
if(!navigator.geolocation) return;
navigator.geolocation.getCurrentPosition(function(pos) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//Check result 0
var result = results[0];
//look for locality tag and administrative_area_level_1
var city = "";
var state = "";
for(var i=0, len=result.address_components.length; i<len; i++) {
var ac = result.address_components[i];
if(ac.types.indexOf("locality") >= 0) city = ac.long_name;
if(ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
}
//only report if we got Good Stuff
if(city != '' && state != '') {
console.log(city);
}
}
});
});
})
我想在我的routes文件夹和app.js中访问变量city
,我找不到任何问题的答案。