【发布时间】:2015-12-04 07:32:25
【问题描述】:
我在使用设置文本标签时遇到问题 https://github.com/googlemaps/js-map-label
使用库设置标签的方法如下:
//var marker = ...
var textLabel = new MapLabel({
text: 'some text'
position: position, //google maps LatLng position
map: map,
fontSize: 35,
align: 'right'
});
textLabel.set('position', position);
marker.bindTo('map', textLabel);
marker.bindTo('position', textLabel);
我使用 addGeoJson 方法导入所有数据,但我无权访问标记。有什么办法可以解决这个问题吗?
我需要为显示的每个标记设置一个文本。 这是我当前的实现:
map.data.addGeoJson(response.data, {idPropertyName: "id"});
map.data.setStyle(function(feature){
var color = feature.getProperty('color');
var zIndex = feature.getProperty('zIndex');
if(feature.getGeometry().getType().toLowerCase() == "point"){
return {
icon: globalOptions.textMarkerPath
}
}else{
return {
fillColor: 'rgb(' + color + ')',
strokeColor: 'rgb(' + color + ')',
fillOpacity: 0.4,
strokeOpacity: 1,
strokeWeight: 1,
zIndex: zIndex
}
}
});
map.data.forEach(function(feature){
if(feature.getGeometry().getType().toLowerCase() == "point") {
var textLabel = new MapLabel({
text: feature.getProperty("text"),
position: feature.position,
map: map,
fontSize: 35,
align: 'right'
});
textLabel.set('position', feature.position);
feature.bindTo('map', textLabel);
feature.bindTo('position', textLabel);
feature.setProperty('textLabel', textLabel);
}
});
再次感谢。
编辑 这是一个 geojson 响应的示例(经过修剪的响应):
{
"viewable": true,
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": 11766,
"text": "",
"layer": "limitaimobil",
"color": "35,40,50",
"zIndex": 7,
"area": 448,
"is_associated": false
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
26.1373083033642,
47.7787618059076
],
[
26.1371254511354,
47.778684435143
],
[
26.1370035662918,
47.7789188945034
],
[
26.1371962266472,
47.779000415299
],
[
26.1373083033642,
47.7787618059076
]
]
]
}
},
{
"type": "Feature",
"properties": {
"id": 12541,
"text": "2",
"layer": "limitaparcela",
"color": "51,153,255",
"zIndex": 48,
"area": 0,
"is_associated": false
},
"geometry": {
"type": "Point",
"coordinates": [
26.1372642328728,
47.7785316061887
]
}
}
]
}
}
【问题讨论】:
-
您的 GeoJSON 是什么样的?请提供一个 Minimal, Complete, Tested and Readable example 来证明问题(包括示例 GeoJSON)。
-
添加了geojson示例
标签: javascript google-maps google-maps-api-3 google-maps-markers