所以我在获取getJSON请求以将地址的全局变量传递到URL时遇到麻烦。我已经在google方法下面包含了通过它的API获取所述地址的方法,然后将其设置为全局变量= globalStringAddress。在提示用户同意位置共享后,将发生此功能。
var map, infowindow, globalStringAddress
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 38.907, lng: -77.036},
zoom: 10
});
var infowindow = new google.maps.InfoWindow;
var geocoder = new google.maps.Geocoder;
// // Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
console.log(position.coords.latitude + ", " + position.coords.longitude);
// console.log(poslat + ", " + poslng);
var latlng = {lat: pos.lat, lng: pos.lng};
geocoder.geocode({'location': latlng}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
globalStringAddress = results[0].formatted_address
console.log("address is : "+ globalStringAddress);
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
var globalStringAddress = globalStringAddress.replace(/[\s]/g, '+').replace(/[,]/g, '%2C');
//var civicAPI ="https://www.googleapis.com/civicinfo/v2/representatives?address="+globalStringAddress+"&key=AIzaSyBwA2-va1J2oaO3IhPn2xqItnyUyhkfkqk";
console.log(globalStringAddress);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
infowindow.setPosition(pos);
infowindow.setContent('Cool House Loser');
infowindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infowindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infowindow, map.getCenter());
}
console.log("address is : "+ globalStringAddress)
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
此问题发生在getJSON请求处。我无法以允许执行的方式将globalStringAddress变量传递到URL中。我可能应该提到我已经在initMap函数中对其进行了一些编辑,以使其对URL更友好,但结果始终是https://www.googleapis.com/civicinfo/v2/representatives?address=[object%20Object]&key=xxx。
$(document).ready(function(globalStringAddress) {
$("#officials").click(function(globalStringAddress){
$.getJSON("https://www.googleapis.com/civicinfo/v2/representatives?address="+globalStringAddress+"&key=xxx", function(result){
window.localStorage.setItem('Senator1', result.officials[2].name),
window.localStorage.setItem('Senator2', result.officials[3].name),
Senator1= result.officials[2].name,
Senator2= result.officials[3].name,
$("#lobby").append(
"You're Senators are " + result.officials[2].name + " & " + result.officials[3].name,
" and you're Representative is " + result.officials[4].name),
// console.log(result.officials[3].channels[0].id),
$.each(result.officials , function(k , v){
$("#lobby").append(
// v.name +' - '+ v.address[0].line1 + " and ",
);
});
});
});
});
这是定位问题吗?我应该将globalStringAddress声明为字符串吗?这是同步问题吗?正确方向上的任何箭头都会受到赞赏,因为我对调制有些陌生