如何获取和存储选定的几何特征以用于我的 Azure Maps 应用程序中的其他功能
How can I get and store a selected geom feature to use in other functions in my Azure Maps app
我正在对地图中的要素使用 'click' 事件。我正在使用我应该能够重用的 "global" var,但是当我再次尝试使用该 var 时,它报告 var 是 'undefined'。我一直无法弄清楚这是什么或为什么将其设置为 'undefined' 所以想知道是否有任何方法可以存储 geom 功能的 ID,然后我可以在网页上简单地 "hide"然后再次将该值检索到 select 该功能,而无需再次单击该功能?
// Global vars
var map, shp;
// Called by click event
function getFeatureProps (e) {
shp = e; // store selected feature (e) for use later
...do other things...
alert(shp); // reports [object] as expected
}
// Called from button
function changeFeatureProperties () {
if (shp.shapes[0].getType() == 'Polygon') { // ERROR: shp is undefined
...only if Polygon...
}
else if (shp.shapes[0].getType() == 'Point') {
...only if Point...
}
...do other things...
}
TIA!
瑞克...
查看您的代码,我认为 shp 未定义的唯一原因是在 getFeatureProps 之前调用了 changeFeatureProperties,或者在未单击任何形状时调用了 getFeatureProps。事件是在地图上还是在图层上?
可以使用 ID,并且可以使用 getShapeById 函数通过 ID 检索存储在 DataSource 中的形状。
同样值得注意的是,事件返回的形状可以是 atlas.Shape 或 GeoJSON 要素对象。如果将形状添加到数据源,将返回一个 atlas.Shape 对象,在大多数其他情况下,它将是一个 GeoJSON 特征。要处理此问题,请添加一个 if 语句,如
if(e.shapes[0] instanceof atlas.Shape){
if(e.shapes[0].getType() === 'Polygon'){
...
}
} else {
//Shape is a GeoJSON feature.
if(e.shapes[0].geometry.type === 'Polygon'){
...
}
}
抱歉耽搁了,我一直在处理流行病问题!
我尝试了很多不同的想法,这是唯一一个对我一直有效的想法。
所以我更正了存储
// Upon object onclick function:
sessionStorage.setItem('shape', JSON.stringify(e.shapes[0].toJson()));
To retrieve if required after storage
getTheProps(shape);
function getTheProps(e) {
var properties;
if (e instanceof atlas.Shape) {
alert('Atlas is Shape');
properties = e.shapes[0].getProperties();
pos = e.position;
}
else {
alert('Atlas is Geometry');
properties = e.properties;
pos = e.geometry.position;
}
}
所以这对我来说如愿以偿,如果其他人有此要求,希望对他们有所帮助。
干杯!
瑞克...
我正在对地图中的要素使用 'click' 事件。我正在使用我应该能够重用的 "global" var,但是当我再次尝试使用该 var 时,它报告 var 是 'undefined'。我一直无法弄清楚这是什么或为什么将其设置为 'undefined' 所以想知道是否有任何方法可以存储 geom 功能的 ID,然后我可以在网页上简单地 "hide"然后再次将该值检索到 select 该功能,而无需再次单击该功能?
// Global vars
var map, shp;
// Called by click event
function getFeatureProps (e) {
shp = e; // store selected feature (e) for use later
...do other things...
alert(shp); // reports [object] as expected
}
// Called from button
function changeFeatureProperties () {
if (shp.shapes[0].getType() == 'Polygon') { // ERROR: shp is undefined
...only if Polygon...
}
else if (shp.shapes[0].getType() == 'Point') {
...only if Point...
}
...do other things...
}
TIA!
瑞克...
查看您的代码,我认为 shp 未定义的唯一原因是在 getFeatureProps 之前调用了 changeFeatureProperties,或者在未单击任何形状时调用了 getFeatureProps。事件是在地图上还是在图层上?
可以使用 ID,并且可以使用 getShapeById 函数通过 ID 检索存储在 DataSource 中的形状。
同样值得注意的是,事件返回的形状可以是 atlas.Shape 或 GeoJSON 要素对象。如果将形状添加到数据源,将返回一个 atlas.Shape 对象,在大多数其他情况下,它将是一个 GeoJSON 特征。要处理此问题,请添加一个 if 语句,如
if(e.shapes[0] instanceof atlas.Shape){
if(e.shapes[0].getType() === 'Polygon'){
...
}
} else {
//Shape is a GeoJSON feature.
if(e.shapes[0].geometry.type === 'Polygon'){
...
}
}
抱歉耽搁了,我一直在处理流行病问题!
我尝试了很多不同的想法,这是唯一一个对我一直有效的想法。
所以我更正了存储
// Upon object onclick function:
sessionStorage.setItem('shape', JSON.stringify(e.shapes[0].toJson()));
To retrieve if required after storage
getTheProps(shape);
function getTheProps(e) {
var properties;
if (e instanceof atlas.Shape) {
alert('Atlas is Shape');
properties = e.shapes[0].getProperties();
pos = e.position;
}
else {
alert('Atlas is Geometry');
properties = e.properties;
pos = e.geometry.position;
}
}
所以这对我来说如愿以偿,如果其他人有此要求,希望对他们有所帮助。
干杯! 瑞克...