Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谷歌地图的JavaScript问题_Javascript_Google Maps - Fatal编程技术网

谷歌地图的JavaScript问题

谷歌地图的JavaScript问题,javascript,google-maps,Javascript,Google Maps,我已经尝试创建一个应用程序,它可以在用户单击时在地图上创建一个标记。然后,如果用户单击一个标记,就会显示一个地址 为了完成这一点,我遇到了一个问题,到目前为止还没有找到解决办法。当我第一次单击地图时,会创建第一个标记。然后,当我尝试单击该标记时,什么也没有出现。然后在第二次单击时,会出现第二个标记,如果我单击第二个标记,则第二个标记上会出现应显示在第一个标记上的地址,依此类推。因此,标记地址不知何故被替换,无法找出问题所在 我一直在寻找一个问题,但无法确定是什么导致了这种奇怪的行为。我是Java

我已经尝试创建一个应用程序,它可以在用户单击时在地图上创建一个标记。然后,如果用户单击一个标记,就会显示一个地址

为了完成这一点,我遇到了一个问题,到目前为止还没有找到解决办法。当我第一次单击地图时,会创建第一个标记。然后,当我尝试单击该标记时,什么也没有出现。然后在第二次单击时,会出现第二个标记,如果我单击第二个标记,则第二个标记上会出现应显示在第一个标记上的地址,依此类推。因此,标记地址不知何故被替换,无法找出问题所在

我一直在寻找一个问题,但无法确定是什么导致了这种奇怪的行为。我是JavaScript的新手,所以我不知道它是否与异步请求有关,或者在我忽略的代码中有一些错误

代码如下:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">

</script>
<script type="text/javascript">
    var map;
    var counter;
    var latlng;
    var locationAddress;
    var geocoder;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    latlng = new google.maps.LatLng(46.043830, 14.488864);
    var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    counter = 0;

     google.maps.event.addListener(map, 'click', function(event) {
        map.setCenter(event.latlng);
        codeLatLng(event.latLng);
    placeMarker(event.latLng);
  });
  }

  function placeMarker(location) {
  var clickedLocation = new google.maps.LatLng(location);
  latlng = location;
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  addLocationInfo(marker);
}

function addLocationInfo(marker){
    var infoWindow = new google.maps.InfoWindow({content: locationAddress, size: new google.maps.Size(50, 50)});
    google.maps.event.addListener(marker, 'click', function(){
        infoWindow.open(map, marker);
    });
}

function codeLatLng(latlng) {
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            locationAddress = results[1].formatted_address;
          }
        } else {
          locationAddress = "Neznan naslov";
        }
      });
    }
  }


</script>

var映射;
var计数器;
var latlng;
变量位置地址;
var地理编码器;
函数初始化(){
geocoder=新的google.maps.geocoder();
latlng=新的google.maps.latlng(46.043830,14.488864);
变量myOptions={
缩放:16,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map_canvas”),myOptions);
计数器=0;
google.maps.event.addListener(映射,'click',函数(事件){
地图设置中心(事件设置);
codeLatLng(事件latLng);
地点标记(事件标记);
});
}
功能位置标记(位置){
var clickedLocation=new google.maps.LatLng(位置);
latlng=位置;
var marker=new google.maps.marker({
位置:位置,,
地图:地图
});
addLocationInfo(标记器);
}
函数addLocationInfo(标记器){
var infoWindow=new google.maps.infoWindow({content:locationAddress,size:new google.maps.size(50,50)});
google.maps.event.addListener(标记'click',函数(){
信息窗口。打开(地图、标记);
});
}
功能代码latlng(latlng){
if(地理编码器){
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
locationAddress=结果[1]。格式化的\u地址;
}
}否则{
locationAddress=“Neznan naslov”;
}
});
}
}
如果有人能指出问题的原因或提出更好的解决方案,我将不胜感激


谢谢大家!

问题在于地理编码器是异步的,因此
locationAddress
在您添加了带有未定义内容的
infowindow
后获得其值

必须从地理编码器的回调函数内部调用
addLocationInfo

下面的代码对调用函数的顺序(以及它们的一些参数)进行了一些小的更改,以实现这一点

   var map;
    var counter;
    var latlng;
    var locationAddress;
    var geocoder;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    latlng = new google.maps.LatLng(46.043830, 14.488864);
    var myOptions = {
      zoom: 16,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    counter = 0;

     google.maps.event.addListener(map, 'click', function(event) {
        map.setCenter(event.latlng);
        placeMarker(event.latLng);

  });
  }

  function placeMarker(location) {
  var clickedLocation = new google.maps.LatLng(location);
  latlng = location;
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });
  codeLatLng(location, marker);
}

function addLocationInfo(marker){
    var infoWindow = new google.maps.InfoWindow({content: locationAddress, size: new google.maps.Size(50, 50)});
    google.maps.event.addListener(marker, 'click', function(){
        infoWindow.open(map, marker);
    });
}

function codeLatLng(latlng, marker) {
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
            locationAddress = results[1].formatted_address;
          }
        } else {
          locationAddress = "Neznan naslov";
        }
        addLocationInfo(marker);
      });
    }
  }