【发布时间】:2016-12-01 10:48:58
【问题描述】:
我有一张地图,我在其中添加了一些多边形来指示某些区域(我将它们作为 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;
}
显然后者更接近我的真实代码;-)
如果我只是添加第一个简单的多边形,那么点击事件会立即被捕获。如果我添加 area1 多边形(它有 80 个点),那么单击两个多边形中的 any 大约需要。 1秒。如果我添加area2 多边形(320 个点),那么单击三个多边形中的任何一个都需要 4-5 秒...
在我的真实应用中,我发现它需要长达 45 秒,甚至在地图上有多个此类区域(有很多点)的地方使应用崩溃。
我只绘制用户所在位置附近的区域 - 因此试图限制绘制的点数。
我尝试向多边形添加事件侦听器(请参阅上面的 cmets),但无法触发它。我尝试过: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)
我试图用longclick 事件替换click 事件。长按的反应完全符合预期。但是,单击仍然会锁定屏幕 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
我还尝试在多边形和地图视图本身上设置bubbleParent=false(请参阅代码示例) - 没有观察到明显的差异。
所以我想我需要一种方法来完全禁用对 click 事件的侦听 - 因为 longclick 事件的行为符合预期 - 当它不锁定应用程序时,我可以忍受长按并给出我期望的结果
编辑 (3)
*我发现无论我是否收听click事件,都会有延迟...所以现在最好的解决方案是为这两个事件实现侦听器,然后在所有多边形超出可见屏幕时立即删除它们 - 如果用户返回该区域,只需重新绘制它们。*
/约翰
【问题讨论】:
标签: android event-handling appcelerator polygon appcelerator-mobile