Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 无法从每个循环加载google地图上的两个标记_Javascript_Jquery_Google Maps - Fatal编程技术网

Javascript 无法从每个循环加载google地图上的两个标记

Javascript 无法从每个循环加载google地图上的两个标记,javascript,jquery,google-maps,Javascript,Jquery,Google Maps,我试图在谷歌地图上加载两个标记,但似乎地图加载了两次,我看不到两个标记。下面是代码 var geocoder; var map; geocoder = new google.maps.Geocoder(); // var address = document.getElementById("address").value; // var user='33936357'; $.getJSON("http://api.twitter.c

我试图在谷歌地图上加载两个标记,但似乎地图加载了两次,我看不到两个标记。下面是代码

    var geocoder;
    var map;
    geocoder = new google.maps.Geocoder();
    //    var address = document.getElementById("address").value;
    //      var user='33936357';
    $.getJSON("http://api.twitter.com/1/users/lookup.json?user_id=33936357,606020001&callback=?", function (data) {
      $.each(data, function (i, item) {
        var screen_name = item.screen_name;
        var img = item.profile_image_url;
        var location = item.location;
        geocoder.geocode({
          address: location
        }, function (response, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            var x = response[0].geometry.location.lat(),
              y = response[0].geometry.location.lng();
            var mapOptions = {
              center: new google.maps.LatLng(x, y),
              zoom: 8,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
            var marker = new google.maps.Marker({
              icon: img,
              title: screen_name,
              map: map,
              position: new google.maps.LatLng(x, y)
            });
          } else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      });
    });

我不知道如何解决这个问题,你的地图创建在each循环中。。试试这个:

// setup the map objects
var geocoder = new google.maps.Geocoder();;
var mapOptions = {
      center: new google.maps.LatLng(0, 0), 
      zoom: 8,
       mapTypeId: google.maps.MapTypeId.ROADMAP
};
// added this 
var bounds = new google.maps.LatLngBounds();
// create the map
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

$.getJSON("http://api.twitter.com/1/users/lookup.json?user_id=33936357,606020001&callback=?", function (data) {
  $.each(data, function (i, item) {
    var screen_name = item.screen_name;
    var img = item.profile_image_url;
    var location = item.location;
    geocoder.geocode({
      address: location
    }, function (response, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var x = response[0].geometry.location.lat(),
          y = response[0].geometry.location.lng(); 
        var myLatLng = new google.maps.LatLng(x, y);
        var marker = new google.maps.Marker({
          icon: img,
          title: screen_name,
          map: map,
          position: myLatLng
        });
        bounds.extend(myLatLng);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  });
  map.fitBounds(bounds);
});
现在您将创建一张地图。。将long和lat添加到
LatLngBounds
对象,然后将贴图设置为适合边界


两次加载,因为在每次迭代中调用同一个画布元素的<代码>新谷歌.map .MaP()/代码>。地图的颜色变蓝,位置远离这些标记。我有中心:新谷歌。地图。LatLng(35.74651,39.46289),但位置仍然在地图中间。