使地理编码器同步调用

时间:2015-12-10 15:11:23

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

我有两个隐藏字段,其中包含纬度值和经度值为{ - {1}}。我将使用初始纬度和经度设置变量#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); }); } 的值。这里默认值是由地图中的标记标记的。当我检查控制台时,第三个是先打印,第一个打印在第二个,第二个打印第三。我不明白为什么会这样。我需要根据订单运行。

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

1 个答案:

答案 0 :(得分: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函数之外声明了地图,以防您想要从控制台检查它。

相关问题