【问题标题】:make geocoder to call synchronously使地理编码器同步调用
【发布时间】:2015-12-10 15:11:23
【问题描述】:

我有两个隐藏字段,其中包含纬度值和经度值,id-#latitude_val and #longitude_val。我将使用初始纬度和经度设置变量chicago。如果以上两个字段为空,我将传递地名(此处为静态检查)并通过以下代码定位标记。

function initialize() { 
  chicago = new google.maps.LatLng(51.508742,-0.120850);//default value
  if(($("#latitude_val").val().length >3) ||   ($("#longitude_val").val().length>3))
   {
   chicago =  new google.maps.LatLng($("#latitude_val").val(), $("#longitude_val").val());  
    }
  else
    { 
  geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': 'Abudhabi'}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK)
      {  
       console.log(results[0].geometry.location.lat()+' '+results[0].geometry.location.lng());//if null this should print first
       chicago = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng()); 
      console.log('__'+chicago);//this should print second    
      }
      else
      {
          console.log("Geocode was not successful for the following reason: " + status);
      }  
      }); 
   }
   console.log('****'+chicago);//this should print third
   var mapOptions = { zoom:4, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago }
   map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);  
   var marker=new google.maps.Marker({
    position:chicago,
    map:map,
   draggable:true,
   animation: google.maps.Animation.DROP
   });

   marker.setMap(map);
   });
 }

我已经检查了chicago的值。这里默认值由地图中的标记标记。当我检查控制台时,第三个是第一个打印,第一个在第二个,第二个在第三个.我不明白为什么会这样。我需要根据顺序运行它。

地图显示的是值 (51.508742,-0.120850) 而不是新值

【问题讨论】:

  • 请提供一个Minimal, Complete, Tested and Readable example 来证明这个问题。
  • 地理编码器是异步的,您的 cmets 指示您希望何时打印各种值不正确。
  • @geocodezip 如何制作同步地理编码器??一切都很好,我只想让变量按代码顺序获取值。
  • @geocodezip 我不明白你为什么说打印的值不正确
  • 因为你说的东西会先打印第三次,所以会先打印。

标签: google-maps google-maps-api-3


【解决方案1】:

所以,我猜您可能会从后端填充所述输入,并在它们为空时提供回退。无论哪种方式,您都希望获得一个位置,并且只有在您想要实例化您的地图。

以下应该有效:

var map;
function initialize() {

  var createMapWithLocation=function(myposition) {
    var mapOptions = {
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: myposition
      },
      mymap = new google.maps.Map(document.getElementById("googlemap"), mapOptions),
      marker = new google.maps.Marker({
        position: myposition,
        map: mymap,
        draggable: true,
        animation: google.maps.Animation.DROP
      });
      console.log('****' + myposition); //this should print third
    return mymap;
  };

  var chicago = new google.maps.LatLng(51.508742, -0.120850); //default value
  if (($("#latitude_val").val().length > 3) || ($("#longitude_val").val().length > 3)) {
    chicago = new google.maps.LatLng($("#latitude_val").val(), $("#longitude_val").val());
    map = createMapWithLocation(chicago);
  } else {
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
      'address': 'Abudhabi'
    }, function (results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        console.log(results[0].geometry.location.lat() + ' ' + results[0].geometry.location.lng()); //if null this should print first
        chicago = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
        console.log('__' + chicago); //this should print second 
         map = createMapWithLocation(chicago);  
      } else {
        console.log("Geocode was not successful for the following reason: " + status);
      }
    });
  }
}

您决定何时致电createMapWithLocation。如果输入被填充,它将是同步的,如果不是,它将是异步的。无论哪种方式,您都不需要事先知道芝加哥的位置。

请注意,我在 initialize 函数之外声明了 map,以防万一您想从控制台检查它。

【讨论】:

    猜你喜欢
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多