با سلام و احترام
من ی از نقشه leaflet در پروژم استفاده میکنم و مشکلی که دارم میخوام یک فایل kml رو روی نقشه نمایش بدم با دستور زیر :
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', 'http://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
fetch('assets/rriver.kml', {
method: 'GET',
mode: 'no-cors',
headers: headers
})
.then(res => res.text())
.then(kmltext => {
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, 'text/kml');
const track = new L.KML(kml);
map.addLayer(track);
const bounds = track.getBounds();
map.fitBounds(bounds);
});
بعد از اجرا خطای زیر رو میده :
Access to XMLHttpRequest at 'http://maps.googleapis.com/maps/api/mapsjs/gen_204?csp_test=true' from origin 'http://localhost:50177' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
خیلی سرچ زدم اما موفق به حل مشکل نشدم، لطفا راهنماییم کنید
خطای قبلی به دلیل این بود که از یک url میخواست فایل رو بخونه، من ادرس لوکال دادم اما میگه فایل رو پیدا نمیکنم:
Failed to load resource: the server responded with a status of 404 (Not Found)
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', 'https://drive.google.com');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
fetch('https://drive.google.com/uc?export=download&id=1cM6o7D1sHUp2pUJNI3cc2dsmbcL-eTfB', {
method: 'GET',
mode: 'no-cors',
headers: headers
})
.then(res => res.text())
.then(kmltext => {
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, 'text/kml');
const track = new L.KML(kml);
dashbord_map.addLayer(track);
// Adjust map to show the kml
const bounds = track.getBounds();
dashbord_map.fitBounds(bounds);
});
هدر رو به شکل بالا تنظیم کردم و الان خطای زیر رو نشون میده :
js.js:10253 Uncaught (in promise) TypeError: Failed to execute 'parseFromString' on 'DOMParser': The provided value 'text/kml' is not a valid enum value of type SupportedType.
با سلام
دوستان کسی نظری یا پیشنهادی نداره، در ادامه آزمون و خطا پیش رفتن(چون سوادشو ندارم) به این رسیدم:
fetch('assets/rriver.kml')
.then(res => res.text())
.then(kmltext => {
// Create new kml overlay
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, 'text/xml');
const track = new L.KML(kml);
map.addLayer(track);
// Adjust map to show the kml
const bounds = track.getBounds();
map.fitBounds(bounds);
});
اما میگه فایل kml رو پیدا نمیکنم:
Failed to load resource: the server responded with a status of 404 (Not Found)
؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)