Google マップにポリゴンを表示したい。
私は2つのことをしました:
- JSON ファイルから座標を読み取る (バージョン 1)
- 変数から座標を読み取る (バージョン 2)
残念ながらバージョン 2 しか動作しません。私が設定した場合paths: paths2
、表示されるのは単純なマップだけです。私が使用すると、paths: paths
すべて正常に動作します。
「paths」と「paths2」の違いを確認するために Firebug を使用しましたが、違いはないようです。
コードは次のとおりです。
//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);