因此,我很难获取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函数中对其进行了一些编辑,以使其更加友好,但结果是https://www.googleapis.com/civicinfo/v2/prepresentatives?address= [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声明为字符串吗?这是同步问题吗?任何朝正确方向的箭头都非常感谢,因为我对调制有些新奇
使用 JSON.stringify
:
$.getJSON("https://www.googleapis.com/civicinfo/v2/representatives?address=" + JSON.stringify(globalStringAddress) + "&key=xxx", function(result) {...});
,所以我通过将下部jQuery嵌套到initmap()函数中来工作。我认为问题是我缺乏光泽的方法来声明函数内部的GlobalStringAddress变量全局?idk,但现在填充到URL中,现在可以