1

I want to show a polygon on a Google Map.

I have done two things:

  • Read the coordinates from a JSON file (Version 1)
  • Read the coordinates from a variable (Version 2)

Unfortunately only Version 2 works. If I set paths: paths2 there is just the plain map to see. If I use paths: paths everything works fine.

I used Firebug the check how 'paths' and 'paths2' differ but there seems to be no difference.

Here is the code:

//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);
4

1 回答 1

0

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

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

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

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

例如。

于 2012-09-20T12:23:05.030 回答