[英]Pulling GeoJSON data into Leaflet with AJAX call
I am new to Leaflet and I was wondering how to load markers from a MySQL database onto a leaflet map. 我是Leaflet的新手,我想知道如何将MySQL数据库中的标记加载到Leaflet映射上。 How to load markers from MySQL using PHP and ajax? 如何使用PHP和Ajax从MySQL加载标记?
.....{"type":"FeatureCollection","features":[{"geometry":{"type":"Point","coordinates":[-122.340141,47.60894]},"properties":{"name":"Pan
Africa Market","address":"1521 1st Ave, Seattle,WA","type":"restaurant"}},.......
Leaflet code: 宣传单代码:
<script>
var map = L.map('map').setView([47.6145, -122.3418], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
$.ajax({
type: "GET",
url: "./geojson.php",
dataType: "geojson",
success: function(response) {
L.geoJson(response, {
onEachFeature: onEachFeature
}).addTo(map);
}
}).error(function() {});
function onEachFeature(feature, layer) {
layer.bindPopup(feature.properties.popupContent);
}</script>
The markers dont show on the map. 标记不会显示在地图上。 I tried type: "POST"
too, I checked chrome console for errors and I got none. 我也尝试type: "POST"
,我检查了chrome控制台是否有错误,但没有得到。
UPDATED geojson.php output: 更新的 geojson.php输出:
{"type":"FeatureCollection","features":[{"geometry":{"type":"Point","coordinates":[-122.340141,47.60894]},"properties":{"name":"Pan Africa Market","address":"1521 1st Ave, Seattle, WA","type":"restaurant"}},{"geometry":{"type":"Point","coordinates":[-122.344391,47.61359]},"properties":{"name":"Buddha Thai & Bar","address":"2222 2nd Ave, Seattle, WA","type":"bar"}},{"geometry":{"type":"Point","coordinates":[-122.356445,47.624561]},"properties":{"name":"The Melting Pot","address":"14 Mercer St, Seattle, WA","type":"restaurant"}},{"geometry":{"type":"Point","coordinates":[-122.337654,47.606365]},"properties":{"name":"Ipanema Grill","address":"1225 1st Ave, Seattle, WA","type":"restaurant"}},{"geometry":{"type":"Point","coordinates":[-122.345673,47.612823]},"properties":{"name":"Sake House","address":"2230 1st Ave, Seattle, WA","type":"bar"}},{"geometry":{"type":"Point","coordinates":[-122.340363,47.605961]},"properties":{"name":"Crab Pot","address":"1301 Alaskan Way, Seattle, WA","type":"restaurant"}},{"geometry":{"type":"Point","coordinates":[-122.345467,47.613976]},"properties":{"name":"Mama's Mexican Kitchen","address":"2234 2nd Ave, Seattle, WA","type":"bar"}},{"geometry":{"type":"Point","coordinates":[-122.326584,47.617214]},"properties":{"name":"Wingdome","address":"1416 E Olive Way, Seattle, WA","type":"bar"}},{"geometry":{"type":"Point","coordinates":[-122.342834,47.610126]},"properties":{"name":"Piroshky Piroshky","address":"1908 Pike pl, Seattle, WA","type":"restaurant"}}]}
Finally I figured out: there was a missing line in the: 最后我弄清楚了:中缺少一行:
$feature = array(
'type' => 'Feature',)
I suspect that the error is here: dataType: "geojson"
我怀疑错误在这里: dataType: "geojson"
jQuery's AJAX supports these values for dataType
: xml
, json
, script
, and html
(from the documentation ). jQuery的AJAX支持dataType
这些值: xml
, json
, script
和html
(来自文档 )。 It will try to parse the response based on that, and "geojson" is not a value it knows or recognizes. 它将尝试基于此解析响应,并且“ geojson”不是它知道或不认识的值。 dataType
is also not to be confused with contentType
and mimeType
, these are different things. dataType
也不要与contentType
和mimeType
混淆,这是不同的东西。
What you simply need to do is change that to dataType: "json"
since GeoJSON files are just JSON. 您只需要做的就是将其更改为dataType: "json"
因为GeoJSON文件只是JSON。
You received no feedback on errors because there was nothing happening in the .error
function. 您没有收到有关错误的反馈,因为.error
函数没有任何反应。
$.ajax({
type: "GET",
url: "./geojson.php",
dataType: "json",
success: function(response) {
L.geoJson(response, {
onEachFeature: onEachFeature
}).addTo(map);
},
error: function(response) {
console.log(response);
}
}).error(function(response) { console.log(response) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.