jQuery和谷歌地图 - 奇怪的行为

时间:2012-09-20 12:16:29

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

我想在Google地图上显示多边形。

我做了两件事:

  • 从JSON文件(版本1)
  • 读取坐标
  • 从变量(版本2)
  • 中读取坐标

不幸的是只有版本2有效。如果我设置paths: paths2,只有普通地图可以查看。如果我使用paths: paths,一切正常。

我使用Firebug检查'paths'和'paths2'的不同之处,但似乎没有区别。

以下是代码:

//Triangle
    var bermudaTriangle;

//Create Map
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);

    //Version 1 (Load from JSON-file)
        var paths2 = [];    
        var geojson2 = $.getJSON('./bermuda.json',function(data){
            var foo = data.coordinates;

            $.each(data.coordinates, function (i, n) { 
                var ll = new google.maps.LatLng(n[0], n[1]); 
                paths2.push(ll);
            });
        });

    //Version 2 (Load from Variabel)
        var paths = [];
        var geojson = {"coordinates":[[25,-80],[18,-66],[32,-64],[25,-85]]};

        $.each(geojson.coordinates, function (i, n) { 
            var ll = new google.maps.LatLng(n[0], n[1]); 
            paths.push(ll);
        });

    //Checkpoint for debugging (The variables are the same!!)
        paths;
        paths2;

        bermudaTriangle = new google.maps.Polygon({
            paths: paths,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#FF0000",
            fillOpacity: 0.35
        });

        bermudaTriangle.setMap(map);

1 个答案:

答案 0 :(得分:0)

好像它没有得到json文件。如果文件位于同一文件夹级别,则删除./

$.getJSON("bermuda.json",function(data){

或者如果它是一个文件夹,那么

$.getJSON("json/bermuda.json",function(data){

例如。