多个多边形,InfoWindow,setMap

时间:2012-12-14 09:03:29

标签: javascript google-maps-api-3

好吧,我已经花了一段时间在这上面了,而且我偶然发现并在许多教程和示例的帮助下实现了这一目标。

我需要创建多个多边形,每个多边形都有一个单独的InfoWindow。想想我已经有了这个但是遇到了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>

4 个答案:

答案 0 :(得分:1)

Javascript没有对像php这样的键的引用,因此无法设置国家['UK']。你必须使用数字键。所以语法应该是这样的:

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

我让你的榜样有效:

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

可以微调。这不完美。这只是一个快速解决方案。 你可以从那里继续

答案 1 :(得分:0)

Javascript数组没有.setMap()方法。 google.maps.Polygon()对象可以。此外,javascript中没有关联数组,例如国家/地区[&#39;英国&#39;]

这可能有效:

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

但它不会是正确的。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

您的代码中存在两个问题:

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