我一直在開發一個讀取.csv文件的應用程序,取出大約16個經度/緯度點,並將它們繪製在Google地圖上。Android上的Google地圖滯後
但是,似乎這些點不喜歡直到我在大約一分鐘後觸摸屏幕(我沒有建立聽衆)[這是一個單獨的問題,也許有人可以回答]。
獲取緯度/經度點並繪製它們的任務被放入AsyncTask doInBackground方法中。刷新地圖的可繪製狀態是在AsyncTask的onPostExecute方法中完成的。
我想我會消除滯後,因爲我有AsyncTask方法爲我工作。當程序啓動時,它會顯示一張空白地圖,我等待大約15秒,如果我觸摸地圖,這些點將插入。但是,此時地圖是難以忍受 laggy!這需要應用至少 5秒到我的相互作用(即縮放,滾動)作出迴應,而且即便如此,它的動作很慢...
做任何事情想他們知道這樣做的原因是什麼?
這裏是我的大部分代碼:在onPostExecute
package net.learn2develop.GoogleMaps;
-- imports here
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
//GeoPoint p;
//GeoPoint p2;
GeoPoint[] p99 = new GeoPoint[16];
public static String[][] bump = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
@SuppressWarnings("deprecation")
View zoomView = mapView.getZoomControls();
mapView.setStreetView(true);
mapView.setSatellite(false);
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
// Add points from ReadCsv.java
/** try/catch to-> async was cut from here */
new MapPoint().execute(bump);
mapView.invalidate();
}
public String[][] getArray(BufferedReader bufRdr) {
-- my method that I know works
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
class MapOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
// -- array of points---
for(int i = 0; i < bump.length; i++) {
Point screenPts99 = new Point();
mapView.getProjection().toPixels(p99[i], screenPts99);
Bitmap bmp99 = BitmapFactory.decodeResource(
getResources(), R.drawable.redpin);
canvas.drawBitmap(bmp99, screenPts99.x, screenPts99.y-44, null);
}
return true;
}
}
private class MapPoint extends AsyncTask <String[][], String, String> {
@Override
protected String doInBackground(String[][]... voidThisArray) {
String voidThisString = null;
try {
InputStream is = getAssets().open("Training4.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
bump = getArray(reader);
if(bump == null){
setContentView(R.layout.deleteme);
} else {
for(int i = 0; i < bump.length; i++) {
String coordinates99[] = {bump[i][0], bump[i][1]};
double lat99 = Double.parseDouble(coordinates99[0]);
double lng99 = Double.parseDouble(coordinates99[1]);
p99[i] = new GeoPoint(
(int) (lat99 * 1E6),
(int) (lng99 * 1E6));
MapOverlay mapOverlay99 = new MapOverlay();
List<Overlay> listOfOverlays99 = mapView.getOverlays();
listOfOverlays99.add(mapOverlay99);
mapView.refreshDrawableState();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return voidThisString;
}
@Override
protected void onPostExecute(String voidThisString) {
super.onPostExecute(voidThisString);
mapView.refreshDrawableState();
mapView.invalidate();
}
}
}
檢查出另一個答案 – Pratik