【发布时间】:2017-02-02 10:04:30
【问题描述】:
我正在以下位置引用 geoJson 文件,以将状态多边形添加到印度地图
https://raw.githubusercontent.com/geohacker/india/master/state/india_telengana.geojson
地图正在使用 googlemaps api
我现在想做的是让每个多边形成为可点击的事件。
在研究中我发现:
- 关于 stackoverflow 的类似查询尚未得到解答
- Leaflet 地图有很多帮助,但 googlemaps 没有
- 有一些建议表明诸如此类的 JSON 文件是数组,但是这些示例中 JSON 文件的构成似乎比我正在访问的要简单得多。在其他示例中很容易看到数组标识符。这些查询也只是试图从数组中返回一个属性,而不是将数组用作可点击事件。
我现在已经创建了一个 JSFiddle,但我无法加载最终的地图(它在小提琴之外加载):
https://jsfiddle.net/everare/df6jbuft/
此处的代码显示我尝试使用 googlemaps https://developers.google.com/maps/documentation/javascript/combining-data 的指导调用事件
HTML:
<div class="container">
<div id="map"style="width:700px;height:700px;border:10px solid black;"> </div>
</div>
Javascript:
//Map construction
var map;
function initMap() {{
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 22.71, lng: 82.48},
zoom: 5,
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
},
styles:[
{ elementType: 'geometry', stylers: [ {color: '#242f3e'}]},
{ elementType: 'labels.text.fill', stylers: [{color: '#746855'}]},
{ elementType: 'labels.text.stroke', stylers: [{color: '#242f3e'}]},
{ featureType: 'administrative', elementType: 'geometry', stylers: [{visibility: 'off'}]},
{ featureType: 'administrative.locality',elementType: 'labels.text.fill',stylers: [{color: '#d59563'}]},
{ featureType: 'poi', stylers: [{visibility: 'off'}]},
{ featureType: 'poi', elementType: 'labels.text.fill',stylers: [{color: '#d59563'}]},
{ featureType: 'poi.park', elementType: 'geometry', stylers: [{color: '#263c3f'}] },
{ featureType: 'poi.park', elementType: 'labels.text.fill', stylers: [{color: '#6b9a76'}]},
{ featureType: 'road', stylers: [{visibility: 'off'}]},
{ featureType: 'road', elementType: 'geometry', stylers: [{color: '#38414e'}]},
{ featureType: 'road', elementType: 'geometry.stroke', stylers: [{color: '#212a37'}]},
{ featureType: 'road', elementType: 'labels.icon', stylers: [{visibility: 'off'}]},
{ featureType: 'road', elementType: 'labels.text.fill', stylers: [{color: '#9ca5b3'}]},
{ featureType: 'road.highway', elementType: 'geometry', stylers: [{color: '#746855'}]},
{ featureType: 'road.highway', elementType: 'geometry.stroke',stylers: [{color: '#1f2835'}]},
{ featureType: 'road.highway', elementType: 'labels.text.fill', stylers: [{color: '#f3d19c'}]},
{ featureType: 'transit', stylers: [{visibility: 'off'}]},
{ featureType: 'transit', elementType: 'geometry', stylers: [{color: '#2f3948'}]},
{ featureType: 'transit.station', elementType: 'labels.text.fill', stylers: [{color: '#d59563'}]},
{ featureType: 'water', elementType: 'geometry',stylers: [{color: '#17263c'}]},
{ featureType: 'water', elementType: 'labels.text.fill', stylers: [{color: '#515c6d' }]},
{ featureType: 'water', elementType: 'labels.text.stroke', stylers: [{color: '#17263c'}]
}
]
});
}
// Loads the state boundary polygons from a GeoJSON source.
function loadMapShapes() {
map.data.loadGeoJson('https://raw.githubusercontent.com/geohacker/india/master/state/india_telengana.geojson', { idPropertyName: 'STATE' });
}
//Responds to the mouse-in event on a map shape (state).
//@param {?google.maps.MouseEvent} e
function mouseInToRegion(e) {
// set the hover state so the setStyle function can change the border
e.feature.setProperty('state', 'hover');
}
//Polygon style
map.data.setStyle({
fillColor: '#FF8000',
strokeWeight: 1
});
// set up events for google.maps.Data
map.data.addListener('mouseover', mouseInToRegion);
// state polygons only need to be loaded once, do them now
loadMapShapes();
}
CSS:
body {
background-color: white;
background-repeat: no-repeat;
background-position: 0px 0px;
}
.container {
position: absolute;
top: 100px;
left: 60px;
【问题讨论】:
-
有什么代码可以看看你到目前为止做了什么吗?您向给定地址发出 GET 请求以访问数据?
-
添加了一些代码。抱歉,您介意详细说明 GET 请求吗?
-
HTTP GET 请求是一种将外部数据检索到应用程序中的方法。更多在这里:en.wikipedia.org/wiki/…我会尝试检查今天晚些时候或明天发生的事情,如果我发现任何有用的信息,请告诉你。
标签: javascript json google-maps-api-3