Javascript 为什么在使用Google Maps API时,全局设置JS变量显示为未定义?

Javascript 为什么在使用Google Maps API时,全局设置JS变量显示为未定义?,javascript,jquery,google-maps-api-3,google-maps-markers,Javascript,Jquery,Google Maps Api 3,Google Maps Markers,我正在尝试使用谷歌地图API地理代码。下面是完整的代码,我第一次尝试时它就起作用了,但现在它没有打开地图,我也不知道为什么 我甚至尝试添加“alert(data.status);”并返回“OK”状态 当我检查时,控制台显示此处未定义“纬度”: var myLatLng = {lat: latitude, lng: longitude}; 加载DOM时设置的变量“latitude”不应该转移到上面的那一行吗?或者我是否对变量范围做了一些错误的操作 <style>

我正在尝试使用谷歌地图API地理代码。下面是完整的代码,我第一次尝试时它就起作用了,但现在它没有打开地图,我也不知道为什么

我甚至尝试添加“alert(data.status);”并返回“OK”状态

当我检查时,控制台显示此处未定义“纬度”:

 var myLatLng = {lat: latitude, lng: longitude};
加载DOM时设置的变量“latitude”不应该转移到上面的那一行吗?或者我是否对变量范围做了一些错误的操作

  <style>
        #map {
        height:200px;
        width: 200px;
        }
    </style>
    <script>
        $(function() {
            var jsonLatLng ='https://maps.googleapis.com/maps/api/geocode/json?address=Indianapolis&sensor=false';
            $.getJSON(jsonLatLng, function (data) {
                if (data.status="OK") {
                latitude = data.results[0].geometry.location.lat;
                longitude = data.results[0].geometry.location.lng;
                } else { //A default lat/long so SOMETHING shows
                latitude = 38.99;
                longitude = -74.80;
                }
            });
        });
    </script>

#地图{
高度:200px;
宽度:200px;
}
$(函数(){
var jsonlatng=https://maps.googleapis.com/maps/api/geocode/json?address=Indianapolis&sensor=false';
$.getJSON(jsonLatLng,函数(数据){
如果(data.status=“OK”){
纬度=数据。结果[0]。geometry.location.lat;
经度=数据。结果[0]。geometry.location.lng;
}else{//一个默认的lat/long,以便显示一些内容
纬度=38.99;
经度=-74.80;
}
});
});
然后这个:

<div id="map"></div>
    <script>
      function initMap() {
        var myLatLng = {lat: latitude, lng: longitude};

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: myLatLng
        });

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: ''
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD98yPUHrWE8QZkSxWR1Fno3_yd70cPQbA&callback=initMap">
    </script>

函数initMap(){
var mylatng={lat:纬度,lng:经度};
var map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:myLatLng
});
var marker=new google.maps.marker({
职位:myLatLng,
地图:地图,
图标:“”
});
}

您没有在代码中的任何地方声明可变经度和纬度。所以你可以这样做:

var longitude, latitude;
$(function() {
  var jsonLatLng ='https://maps.googleapis.com/maps/api/geocode/json?address=Indianapolis&sensor=false';
  $.getJSON(jsonLatLng, function (data) {
      if (data.status="OK") {
      latitude = data.results[0].geometry.location.lat;
      longitude = data.results[0].geometry.location.lng;
      } else { //A default lat/long so SOMETHING shows
      latitude = 38.99;
      longitude = -74.80;
      }
  });
});

此外,如果将var放在函数中,变量将具有函数作用域,这意味着它将仅在函数中可用

两个异步函数之间存在竞争条件:

  • ajax地理编码器调用
  • Google Maps Javascript API v3的异步加载(
    async defer&callback=initMap
  • 最简单的解决方法是在initMap函数中调用geocoder(在加载API之后)

    html,
    身体,
    #地图{
    身高:100%;
    宽度:100%;
    填充:0px;
    边际:0px;
    }
    
    
    您没有声明变量经度和纬度您说
    纬度
    是一个全局变量,但根据您发布的代码,它不是全局变量。谢谢。快速跟进。我无法在另一个函数中使用纬度或经度,即使我在上面声明类似于您拥有它。换句话说,我将“console.log(latitude);”放在“if”部分,它显示了一个正确的数字,但是如果我将“console.log(latitude);”移到$.getJSON函数之外,它将显示为未定义。既然我在主函数之外声明了它,那么它不应该到处定义吗?它应该到处声明。你能装一把小提琴吗?我在这里装了一把:,但我不知道如何把这个部分装进小提琴
    。我把它添加为一个外部资源URL,但我不认为这是正确的
    function initMap() {
      var jsonLatLng = 'https://maps.googleapis.com/maps/api/geocode/json?address=Indianapolis';
      $.getJSON(jsonLatLng, function(data) {
        if (data.status == "OK") { // <======= fixed typo on this line
          latitude = data.results[0].geometry.location.lat;
          longitude = data.results[0].geometry.location.lng;
        } else { //A default lat/long so SOMETHING shows
          latitude = 38.99;
          longitude = -74.80;
        }
        var myLatLng = {
          lat: latitude,
          lng: longitude
        };
    
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: myLatLng
        });
    
        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: ''
        });
      });
    }