我有一个地图,我添加一些多边形来指示一些区域(我把它们作为GeoJSON格式)。当用户点击多边形时,我向用户显示有关该区域的一些信息。
当多边形中的点数增加时,这在Android上会变慢 - 甚至有时会使应用程序崩溃。
这是我加载地图的方式:
mapView = Map.createView({
mapType:Map.SATELLITE_TYPE,
userLocation:true,
rotateEnabled: false,
bubbleParent:false // Edit 2
});
mapView.addEventListener('click',clickHandler);
$.map.add(mapView);
然后我设置区域并绘制一些像这样的多边形:
Ti.API.info('Set region and draw polygon: e.coords=' + JSON.stringify(e.coords));
mapView.setRegion({
latitude:e.coords.latitude,
longitude:e.coords.longitude,
latitudeDelta:(currentLatDelta > 0.02 ? 0.02 : currentLatDelta),
longitudeDelta:(currentLonDelta > 0.02 ? 0.02 : currentLonDelta)
});
mapView.addPolygon(getPolygon(e.coords));
mapView.addPolygon(getPolygonArea(area1));
mapView.addPolygon(getPolygonArea(area2));
getPolygon
方法是一种简单的测试方法:
function getPolygon(pt){
// return a polygon relative to the point
var points = [];
points.push({latitude:pt.latitude - 0.001, longitude:pt.longitude - 0.0012});
points.push({latitude:pt.latitude - 0.001, longitude:pt.longitude + 0.0012});
points.push({latitude:pt.latitude, longitude:pt.longitude + 0.002});
points.push({latitude:pt.latitude + 0.001, longitude:pt.longitude + 0.0012});
points.push({latitude:pt.latitude + 0.001, longitude:pt.longitude - 0.0012});
points.push({latitude:pt.latitude, longitude:pt.longitude - 0.002});
points.push({latitude:pt.latitude - 0.001, longitude:pt.longitude - 0.0012});
Ti.API.info('getPolygon: points=' + JSON.stringify(points));
var poly = Map.createPolygon({
points: points,
id:'poly',
fillColor: '#55ec858f',
strokeColor: 'black',
strokeWidth: 1,
bubbleParent:false // Edit 2
});
/* --> This event never triggers
poly.addEventListener('singleTap', function(e){
alert('Clicked inside: ' + e.shape.id);
e.bubbleParent = false;
});
*/
return poly;
}
而getPolygonArea
方法只是根据GeoJSON数据结构(包含所有点)创建多边形:
function getPolygonArea(area){
var points = [];
var i = 0;
_.each(area.geometry.coordinates[0][0], function(point) {
points.push({
latitude : point[0],
longitude : point[1]
});
i++;
});
console.info("Added " + i + " points for: " + area.properties.NAVN);
var poly = Map.createPolygon({
points : points,
id : area.properties.NAVN,
fillColor : '#66FF0000',
strokeColor : 'black',
strokeWidth : 1,
bubbleParent:false // Edit 2
});
return poly;
}
显然后者更接近我的真实代码;-)
如果我只是添加第一个简单多边形,则会立即捕获click事件。如果我添加area1
多边形(它有80个点),那么点击两个多边形中的任何一个都需要大约。 1秒。如果我添加area2
多边形(320点),那么点击三个多边形中的任何一个需要4-5秒......
在我的真实应用程序中,我看到它需要长达45秒甚至在地图上有多个这些区域的地方崩溃应用程序(有很多点)。
我只绘制用户所在位置附近的区域 - 因此试图限制绘制的点数。
我试图向多边形添加一个事件监听器(参见上面的注释),但是无法触发它。我尝试过:click
,polygonClick
和singleTap
(在我寻找解决方案后提到了各种提及)。
因此,似乎地图的全局事件处理程序是我必须使用的。这触发了 - 但延误......
在iOS上 - 它只是工作。该事件立即触发。
我该怎么做才能在Android上运行这项工作?
我在:
Mac OS 10.12.1
Appcelerator Studio, build: 4.8.0.201611120604
Titanium SDK: 5.5.0.GA
Test device: Nexus 5 - Android 6
提前致谢!
编辑
我试图将我的环境升级到最新版本,看看是否有任何影响:
Appcelerator Studio, build: 4.8.1.201611291132
Titanium SDK: 6.0.0.GA
ti.map: 3.1.0 for Android
但唉它没有什么区别 - 仍然死得很慢:-(
如果您不知道GeoJSON格式,那么您可以看到example here(我正在绘制的区域)
编辑(2)
我试图用click
事件替换longclick
事件。并且长按完全按预期反应。但是,单击仍然会锁定屏幕4-5秒。
因此,我看了一下Android设备监视器中的LogCat
,它显示了这样的条目似乎是相关的(参见例如.here):
12-05 12:28:44.406: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2530.9ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=0, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (203.0, 788.0)]), policyFlags=0x62000000
12-05 12:28:44.407: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2524.3ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (205.0, 790.0)]), policyFlags=0x62000000
12-05 12:28:44.408: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2516.3ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (217.0, 793.0)]), policyFlags=0x62000000
12-05 12:28:44.409: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2507.7ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (237.0, 798.0)]), policyFlags=0x62000000
12-05 12:28:44.410: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2499.4ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (255.0, 804.0)]), policyFlags=0x62000000
12-05 12:28:44.411: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2491.1ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (277.0, 813.0)]), policyFlags=0x62000000
12-05 12:28:44.412: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2483.1ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (297.0, 822.0)]), policyFlags=0x62000000
12-05 12:28:44.413: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2474.6ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (322.0, 835.0)]), policyFlags=0x62000000
12-05 12:28:44.414: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2466.5ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (343.0, 847.0)]), policyFlags=0x62000000
12-05 12:28:44.415: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2457.9ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (363.0, 859.0)]), policyFlags=0x62000000
12-05 12:28:44.417: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2449.7ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (381.0, 869.0)]), policyFlags=0x62000000
12-05 12:28:44.418: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2441.1ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (398.0, 878.0)]), policyFlags=0x62000000
12-05 12:28:44.419: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2433.2ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (408.0, 885.0)]), policyFlags=0x62000000
12-05 12:28:44.420: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2424.7ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (417.0, 891.0)]), policyFlags=0x62000000
12-05 12:28:44.422: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2416.7ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (424.0, 895.0)]), policyFlags=0x62000000
12-05 12:28:44.423: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2408.0ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (430.0, 899.0)]), policyFlags=0x62000000
12-05 12:28:44.424: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2400.0ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (438.0, 905.0)]), policyFlags=0x62000000
12-05 12:28:44.424: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2391.4ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (447.0, 912.0)]), policyFlags=0x62000000
12-05 12:28:44.425: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2383.3ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (456.0, 921.0)]), policyFlags=0x62000000
12-05 12:28:44.426: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2374.7ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (466.0, 929.0)]), policyFlags=0x62000000
12-05 12:28:44.426: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2367.0ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (476.0, 937.0)]), policyFlags=0x62000000
12-05 12:28:44.427: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2358.6ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (484.0, 943.0)]), policyFlags=0x62000000
12-05 12:28:44.428: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2350.8ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (490.0, 948.0)]), policyFlags=0x62000000
12-05 12:28:44.428: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2342.4ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (493.0, 950.0)]), policyFlags=0x62000000
12-05 12:28:44.429: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2334.3ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=2, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (494.0, 951.0)]), policyFlags=0x62000000
12-05 12:28:44.430: I/InputDispatcher(883): Window 'Window{f958ab5 u0 dk.dalsgaarddata.mapdemo/org.appcelerator.titanium.TiActivity}' spent 2348.2ms processing the last input event: MotionEvent(deviceId=7, source=0x00001002, action=1, actionButton=0x00000000, flags=0x00000000, metaState=0x00000000, buttonState=0x00000000, edgeFlags=0x00000000, xPrecision=1.0, yPrecision=1.0, displayId=0, pointers=[0: (494.0, 951.0)]), policyFlags=0x62000000
我还尝试在多边形和mapview本身上设置bubbleParent=false
(参见代码示例) - 没有观察到明显的差异。
所以我想我需要一种方法来完全禁用对click
事件的监听 - 因为longclick
事件的行为符合预期 - 当我没有锁定应用程序并给出我期望的结果时,我可以忍受长时间的点击
编辑(3)
*我发现无论我是否听click
事件都存在延迟...所以现在最好的解决方案是为两个事件实现侦听器,然后在它们位于可见区域之外时删除任何多边形屏幕 - 如果用户返回该区域,只需重绘它们。*
/约翰
它循环遍历地图上的每个叠加层,因此叠加层越多,返回事件的速度就越慢。
我的解决方案是删除我不需要的所有叠加...(在我的情况下,我不需要列出任何叠加,只有注释),并重建模块。不幸的是,我必须始终更新我的自定义模块以删除这些循环并获得Ti.Map中的新功能。