我正嘗試使用Google Maps JS API創建一個小應用程序。我正在使用數據層從GeoJSON文件加載一堆點。該文件似乎正在加載,並且地圖正在顯示,但在map.data.setstyle()中設置的圖標不會顯示...Google Maps API data.setStyle在地圖上不顯示圖標
下面是我的HTML,CSS和JS。我一直在看這個爲期2天,我無法弄清楚發生了什麼問題。先謝謝你!
HTML
<body>
<div id="map-canvas"></div>
</body>
CSS
html {
height: 100%
}
body {
height: 100%;
margin: 0;
padding: 0
}
#map-canvas {
height: 100%
}
JS
$(document).ready(function() {
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(37.000, -120.000),
zoom: 7
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
map.data.loadGeoJson('/map.json');
map.data.setStyle(function(feature) {
var theaterName = feature.getProperty('name');
return {
icon: "https://maps.gstatic.com/mapfiles/ms2/micons/marina.png",
visible: true,
clickable: true,
title: theaterName
};
});
}
google.maps.event.addDomListener(window, 'load', initialize);
});
JSON
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"rentrak_id": "9183", "name": "Palm Theatre", "address": "817 Palm St, San Luis Obispo, CA"},
"geometry": {
"type": "Point",
"coordinates": [35.2815558, -120.6638196]
}
},
{
"type": "Feature",
"properties": {
"rentrak_id": "8961", "name": "King City 3", "address": "200 Broadway St, King City, CA"},
"geometry": {
"type": "Point",
"coordinates": [36.21372, -121.1261771]
}
},
{"type": "Feature", "properties": {
"rentrak_id": "5549", "name": "Van Buren 3 DI", "address": "3035 Van Buren Blvd, Riverside, CA"},
"geometry": {
"type": "Point",
"coordinates": [33.9113137, -117.4364228]
}},
{"type": "Feature", "properties": {
"rentrak_id": "990802", "name": "CGV Cinemas LA", "address": "621 S Western Ave, Los Angeles, CA"},
"geometry": {
"type": "Point",
"coordinates": [34.0626656, -118.3093961]
}},
{"type": "Feature", "properties": {
"rentrak_id": "5521", "name": "Rancho Niguel 7", "address": "25471 Rancho Niguel Rd, Laguna Niguel, CA"},
"geometry": {
"type": "Point",
"coordinates": [33.5560509, -117.68533]
}}]}
編輯::
所以,我的劇本是工作,當我使用這個文件:http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week
這讓我覺得有什麼不對我的本地JSON文件......但是當我通過JavaScript的步驟,我可以查看每個功能並檢查屬性和幾何,並且它們似乎已正確加載到google.maps.feature對象中。所以,我仍然不知道爲什麼這些觀點不會出現。
你的腳本,因爲它是按預期工作對我來說,請創建演示或交'map.json' –
隨着你的腳本和你的JSON文件中的每個功能都有正確的緯度,它被設置爲-90錯誤的經度。至少這是我在console.log中看到的 –
Blahh !!謝謝!是的,經度和緯度的順序是錯誤的,所以沒有任何顯示。 –