現在、私はdatabase1
マップ要素(マーカーポイント、ポリラインポイント、およびポリゴンポイントとその情報)のデータベース(呼び出しは)を持っており、ポリライン/ポリゴンポイントについては、それらのポイント(緯度/経度)を別のデータベースに保持しています(これを呼び出しますdatabase2
) ここで、各ポイントは、 の要素 ID で参照される別個のデータベース アイテムですdatabase1
。
MapFragment が読み込まれると、データベースからすべてをプルします。AsyncTask
c = getActivity().getContentResolver().query(MapElements.CONTENT_URI,new String[] { MapElements.ID,MapElements.LOCATION_STRING,MapElements.LAYER_ID,MapElements.NEW_OR_MOD}, null, null, null);
カーソルをループし、要素がポリラインまたはポリゴンの場合、その要素のすべてのポイントをプルしid
、リストを作成して、後でラインまたはポリゴンの作成に入れます
Cursor c2 = context.getContentResolver().query(MapPoints.CONTENT_URI,new String[] { MapPoints.LAYER_ID, MapPoints.LATITUDE,MapPoints.LONGITUDE },
MapPoints.ELEMENT_ID + "=?",new String[] { String.valueOf(id) }, null);
if (c2 != null && c2.moveToFirst()) {
do {
locationArray.add(new LatLng(c2.getFloat(1), c2.getFloat(2)));
} while (c2.moveToNext());
}
c2.close();
次に、リストを使用してマップにプロットします
Polyline line = map.addPolyline(new PolylineOptions().addAll(mElement.getLocationArray()));
このプロセス全体には時間がかかる場合があります。たとえば、250
さまざまな要素を使用すると、それらすべてをロードするのに約 10 ~ 15 秒かかり、マップ要素の数ははるかに多い場合も少ない場合もあるため、明らかにポイントが多いほど時間がかかります。
Googleマップアプリを見ると、すべてのマーカーがすぐに読み込まれるように見えます.これをスピードアップする方法はありますか??
アップデート
トレースビューから理解できることを少し掘り下げてみたところ、Handler
オペレーションは 2 番目と 3 番目に長く実行されているオペレーションであることがわかりました。だから私がしたことは、メインスレッドにコールバックしてマップに配置するために使用する asynctask のハンドラーを取り出し、プロセスは2秒で完了しました...
ハンドラーコードを取り出して、今のところ独自のメソッドに入れました。これがメソッドです
private void test(final MapElement mElement){
if (mElement.getType() > 0) {
try{
Handler h = new Handler(getActivity().getMainLooper());
if (mElement.getType() == 3) {
h.post(new Runnable(){
public void run() {
Polygon poly = map.addPolygon(new PolygonOptions()
.addAll(mElement.getLocationArray()));
String color = mElement.getFillColor();
String borderColor = mElement.getBorderCOlor();
poly.setFillColor(Color.parseColor("#"+color));
poly.setStrokeColor(Color.parseColor("#"+borderColor));
poly.setStrokeWidth(4);
poly.setVisible(false);
Marker m = map.addMarker(new MarkerOptions()
.position(mElement.getPoint())
.icon(BitmapDescriptorFactory.fromResource(mElement.getMarkerIcon())));
m.setVisible(false);
m.setSnippet(String.valueOf(mElement.getID()));
mElement.setMarker(m);
mElement.setPolygon(poly);
}
});
mapElements.put(mElement.getID(), mElement);
} else if (mElement.getType() == 2) {
h.post(new Runnable(){
public void run() {
Polyline line = map
.addPolyline(new PolylineOptions()
.addAll(mElement.getLocationArray()));
String borderColor = mElement.getBorderCOlor();
if(borderColor == null){
line.setColor(Color.BLUE);
}else{
line.setColor(Color.parseColor("#"+borderColor));
}
line.setWidth(mElement.getThickness());
line.setVisible(false);
if(mElement.getLayerId() != 16){
Marker m = map.addMarker(new MarkerOptions()
.position(mElement.getPoint())
.icon(BitmapDescriptorFactory.fromResource(mElement.getMarkerIcon())));
m.setVisible(false);
m.setSnippet(String.valueOf(mElement.getID()));
mElement.setMarker(m);
}
mElement.setPolyLine(line);
}
});
mapElements.put(mElement.getID(), mElement);
} else {
h.post(new Runnable(){
public void run() {
Marker m = map.addMarker(new MarkerOptions()
.position(mElement.getPoint()).icon(
BitmapDescriptorFactory.fromResource(mElement.getMarkerIcon())));
m.setVisible(false);
m.setSnippet(String.valueOf(mElement.getID()));
mElement.setMarker(m);
}
});
mapElements.put(mElement.getID(), mElement);
}
ContentValues values = new ContentValues();
values.put(MapElements.PLOTTED, 1);
getActivity().getContentResolver().update(Uri.withAppendedPath(MapElements.CONTENT_ID_URI_BASE,String.valueOf(mElement.getID())), values, null, null);
}catch(NullPointerException e){
e.printStackTrace();
}
}
}
ハンドラーを取り出してtest
onPostExecute にメソッドを配置しても、遅延が発生します。この0.058 seconds
メソッドを 1 回完了するには 250 を掛けると、15 seconds
これが問題のようですが、メインスレッドへのこのコールバックを別の方法で処理する必要がありますか?