最近、 AngularJSとLeafletを使用してアプリを作成しました。JSONファイルからの位置データを含め、これまでに説明した内容と非常によく似ています。私の解決策はbleshに似ています。
これが基本的なプロセスです。
自分<map>
のページの1つに要素があります。<map>
次に、要素をリーフレットマップに置き換えるディレクティブがあります。JSONデータをファクトリにロードするため、セットアップは少し異なりますが、ユースケースに合わせて調整しました(エラーがある場合はお詫びします)。ディレクティブ内で、JSONファイルをロードしてから、各場所をループします(互換性のある方法でJSONファイルを設定する必要があります)。次に、各緯度/経度にマーカーを表示します。
HTML
<map id="map" style="width:100%; height:100%; position:absolute;"></map>
指令
app.directive('map', function() {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
link: function(scope, element, attrs) {
var popup = L.popup();
var southWest = new L.LatLng(40.60092,-74.173508);
var northEast = new L.LatLng(40.874843,-73.825035);
var bounds = new L.LatLngBounds(southWest, northEast);
L.Icon.Default.imagePath = './img';
var map = L.map('map', {
center: new L.LatLng(40.73547,-73.987856),
zoom: 12,
maxBounds: bounds,
maxZoom: 18,
minZoom: 12
});
// create the tile layer with correct attribution
var tilesURL='http://tile.stamen.com/terrain/{z}/{x}/{y}.png';
var tilesAttrib='Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.';
var tiles = new L.TileLayer(tilesURL, {
attribution: tilesAttrib,
opacity: 0.7,
detectRetina: true,
unloadInvisibleTiles: true,
updateWhenIdle: true,
reuseTiles: true
});
tiles.addTo(map);
// Read in the Location/Events file
$http.get('locations.json').success(function(data) {
// Loop through the 'locations' and place markers on the map
angular.forEach(data.locations, function(location, key){
var marker = L.marker([location.latitude, location.longitude]).addTo(map);
});
});
}
};
サンプルJSONファイル
{"locations": [
{
"latitude":40.740234,
"longitude":-73.995715
},
{
"latitude":40.74277,
"longitude":-73.986654
},
{
"latitude":40.724592,
"longitude":-73.999679
}
]}