将全局变量应用于$ .getjson请求URL

问题描述 投票:0回答:2

所以我无法获取我的getJSON请求以将地址的全局变量传递到URL。我已经在谷歌方法下面通过它的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/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声明为字符串吗?这是同步问题吗?任何正确方向的箭头都非常受欢迎,因为我对调制有点新意

javascript jquery json get global-variables
2个回答
0
投票

使用JSON.stringify

$.getJSON("https://www.googleapis.com/civicinfo/v2/representatives?address=" + JSON.stringify(globalStringAddress) + "&key=xxx", function(result) {...});

0
投票

所以我通过将较低的jQuery嵌套到initMap()函数中来实现这一点。我认为问题在于我缺乏光泽方法从函数中声明globalStringAddress变量全局? idk但现在填充到URL就好了

© www.soinside.com 2019 - 2024. All rights reserved.