【问题标题】:multiple polygons, InfoWindow, setMap多个多边形、InfoWindow、setMap
【发布时间】:2012-12-14 09:03:29
【问题描述】:

好吧,我在这方面花了一段时间,在许多教程和示例的帮助下,我跌跌撞撞地走到了这一步。

我需要创建多个多边形,每个多边形都有一个单独的信息窗口。认为我已经做到了,但是 setMap 遇到了麻烦。这是我已经走了多远:

遇到类型错误

object #<object> has no method 'setMap'

我该如何解决这个错误?

<script>
var map;
var infoWindow;

function initialize() {
  var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875);
  var mapOptions = {
    zoom: 5,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

map = new google.maps.Map(document.getElementById("map_canvas"),
      mapOptions);

  // Let's start with an empty object:
var countries = {    
};

// Now let's add a county polygon:
countries['UK'] = new google.maps.Polygon({
  paths: [
    new google.maps.LatLng(59.677361, -2.469846),
    new google.maps.LatLng(59.299717,   -6.314917),
    new google.maps.LatLng(57.877247,   -9.314917),
    new google.maps.LatLng(54.428078,  -11.638861),
    new google.maps.LatLng(51.784554,  -11.702241),
    new google.maps.LatLng(50.185848,  -10.054354),
    new google.maps.LatLng(49.405380,   -7.012100),
    new google.maps.LatLng(49.090675,   -3.272664),
    new google.maps.LatLng(48.775970,   -1.709283),
    new google.maps.LatLng(49.757851,   -2.089565),
    new google.maps.LatLng(50.714554,    1.037195),
    new google.maps.LatLng(51.482437,    2.304801),
    new google.maps.LatLng(53.433609,    3.276632),
    new google.maps.LatLng(55.863132,    3.445646)
    // ...
  ],
  strokeColor: "#FF0000",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: "#FF0000",
  fillOpacity: 0.3
});

// Next county:
countries['FR'] = new google.maps.Polygon({
  paths: [
    // This is not real data:
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.757370),
    new google.maps.LatLng(25.774252, -80.190262)
    // ...
  ],
  strokeColor: "#FF0000",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: "#FF0000",
  fillOpacity: 0.3
});

// And so on...

countries.setMap(map)

;
  // Add a listener for the click event
  google.maps.event.addListener(UK, 'click', showInfoUK);
  google.maps.event.addListener(FR, 'click', showInfoFR);

  infowindow = new google.maps.InfoWindow();
}

function showInfoUK(event) {
    var contentString = "<b>UK</b><br />";
        contentString += "UK, Channel Islands, Ireland";



  // Replace our Info Window's content and position
  infowindow.setContent(contentString);
  infowindow.setPosition(event.latLng);

  infowindow.open(map);
}
function showInfoFR(event) {
    var contentString = "<b>FR</b><br />";
        contentString += "France, N,W";



  // Replace our Info Window's content and position
  infowindow.setContent(contentString);
  infowindow.setPosition(event.latLng);

  infowindow.open(map);
}

</script>

【问题讨论】:

  • 你的问题是什么?
  • 该死!我收到 setMap 错误
  • javascript 数组没有 .setMap() 方法。 google.maps.Polygon() 对象可以。此外,javascript 中没有关联数组,例如国家['UK']。
  • 我明白了,那么我完全错了!?

标签: javascript google-maps-api-3


【解决方案1】:

Javascript 没有对诸如 php 之类的键的引用,因此无法设置国家/地区['UK']。您必须使用数字键。所以语法应该是这样的:

countries[0] = new google.maps.Polygon({ ... });
countries[1] = new google.maps.Polygon({ ... });

我让你的例子起作用了:

http://jsfiddle.net/AcCzT/15/

可以微调。这并不完美。这只是一个快速修复。 你可以从那里继续

【讨论】:

  • 啊,我明白了,谢谢。但是,当我上传它时,main.js 出现错误:对象没有方法“加载”
  • 您是否正确包含了 google maps api js? main.js 是 Google 使用的主要文件。你的初始化代码是什么?
  • 嗨,渡轮,我使用小提琴中的代码创建了 300 多个多边形(英国县边界)。但是,我不确定如何为弹出窗口提供他们需要的信息,我目前无法让它弹出。使用这里的代码,它为每个新的信息窗口和一个匹配的函数进行了硬编码。我是否需要在我的 foreach 循环中包含该函数并动态创建它?
【解决方案2】:

Javascript 数组没有.setMap() 方法。 google.maps.Polygon() 对象可以。另外,javascript中没有关联数组,比如country['UK']

这可能有效:

countries['UK'].setMap(map);

但它不会是正确的。

【讨论】:

  • 谢谢,我之前确实尝试过,但它得到了参考错误 uk is not defined。
  • 只有在省略引号时才能收到该错误消息,例如国家[UK],而不是国家['UK'],但无论如何,Javascript 中没有关联数组
【解决方案3】:

您的代码中有两个问题:

  1. 正如 Marcelo 所说,要将多边形添加到地图中,请使用:countries["UK"].setMap(map);
  2. 要添加点击监听,需要引用多边形:google.maps.event.addListener(countries["UK"], 'click', showInfoUK);

working example

【讨论】:

    【解决方案4】:

    使用 ajax 更简单,更简单。 例如,

    downloadUrl("url", function(data) {
        for (var i = 0; i < polygons.length; i++) {
            ........
            var decodedPath = ........
            polygon.setMap(map);
            ........
        }
    });
    

    【讨论】:

    • 不要发布直接链接......在你的答案中写一些代码......或expalin ..
    • 感谢您的示例,但没有太多用处,因为我看不到您的代码,因此无法看到您是如何做到的。
    猜你喜欢
    • 2012-10-12
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 2012-05-29
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多