ajax读取geojson文件,在AJAX中使用JSON代替GeoJSON在使用AJAX的传单中使用

我正在寻找一种使用AJAX代替GeoJSON的方式。使用JSON和AJAX是必需的。在AJAX中使用JSON代替GeoJSON在使用AJAX的传单中使用

我设法使用AJAX调用JSON文件。但是,现在我很困惑我如何使用JSON中的数据在地图上绘制标记。我猜我不能使用L.geoJson()。

HTML:

这是JavaScript文件:

var map;

var overlay;

var addPopupsFromLocations = function(locations) {

var popups = new Array();

locations.forEach(function(location){

console.log('creating popup for location ' + location.title);

console.log(location.latitude);

console.log(location.longitude);

}) ;

};

function init() {

var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {

maxZoom: 18,

attribution: 'Map data © OpenStreetMap contributors, ' +

'CC-BY-SA, ' +

'Imagery © Mapbox',

id: 'examples.map-i86knfo3'

}).addTo(map);

}

$(document).ready(function(){

init();

$.ajax('locations.json', {

dataType: 'json',

success: addPopupsFromLocations,

error: function(xhr, st, et) {

console.warn(et);

}

});

});

这是JSON文件:

[

{

"title": "Weathertop",

"link": "http://en.wikipedia.org/wiki/Weathertop",

"latitude": 51.505,

"longitude": -0.09,

"imageUrl": "assets/img/location-images/Weathertop.jpg"

},

{

"title": "Rivendell",

"link": "http://lotr.wikia.com/wiki/Rivendell",

"latitude": -0.09,

"longitude": 51.505,

"imageUrl": "assets/img/location-images/Rivendell2.jpg"

},

{

"title": "Minas Tirith",

"link": "http://lotr.wikia.com/wiki/Minas_Tirith",

"latitude": 38.78,

"longitude": -77.18,

"imageUrl": "assets/img/location-images/320px-Minas_Tirith.jpg"

}

]

控制台:

creating popup for location Weathertop

custom.js (line 7)

51.505

custom.js (line 9)

-0.09

custom.js (line 10)

creating popup for location Rivendell

custom.js (line 7)

-0.09

custom.js (line 9)

51.505

custom.js (line 10)

creating popup for location Minas Tirith

custom.js (line 7)

38.78

custom.js (line 9)

-77.18

2014-06-19

wag0325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值