I want my JSON data with URL can load on the map, and it is a polygon data.
I don't know what the problem with my code.
This is my code:
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin="" />
<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""></script>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
var osmUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
osmAttrib = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
osm = L.tileLayer(osmUrl, { maxZoom: 20, attribution: osmAttrib });
var map = L.map('map').setView([24.151687694799833, 120.64116954803465], 15).addLayer(osm);
L.control.scale().addTo(map);
var states = $.getJSON("https://www.geologycloud.tw/api/v1/zh-tw/Stratum?t=.json"
, function (data) {
var markerGroup = L.featureGroup();
data.value.forEach(function (itemData, itemInd) {
for (var j = 0; j < states.features.length; j++) {
for (var i = 0; i < states.features[j].geometry.coordinates[0].length; i++) {
var po = [states.features[j].geometry.coordinates[0][i][1], states.features[j].geometry.coordinates[0][i][0]];
var polygon = L.polygon([po]).addTo(map);
}
});
markerGroup.addTo(map);
map.fitBounds(markerGroup.getBounds());
});
</script>
Best Answer
First, I had a CORS error getting the GeoJSON from the link to my map. So I hit the link and copy/pasted the date into a file called poly1.json and put this in the same folder as my page. It works. But the data source is now static not dynamic.
Your making it look difficult, try this code: