android – 添加许多叠加后MapView平移和缩放很慢

android – 添加许多叠加后MapView平移和缩放很慢,第1张

概述所以我花了很多时间试图弄清楚如何加快速度,但我现在已经没想到了.我有一个类mapPopup,其中MapView显示在整个屏幕上. mapPopup中有一个GeoPoint数组的数组,我想在数组的第二维中的每个GeoPoint之间绘制线条.我已经使用自定义类mapOverlay完成了这个任务,它扩展了Overlay,但我遇到的问题是,一旦绘制了所有的地图叠加层,地图的缩放平移速度都非常慢.将所有叠 @H_404_2@ 所以我花了很多时间试图弄清楚如何加快速度,但我现在已经没想到了.我有一个类mapPopup,其中MapVIEw显示在整个屏幕上. mapPopup中有一个GeoPoint数组的数组,我想在数组的第二维中的每个GeoPoint之间绘制线条.我已经使用自定义类mapOverlay完成了这个任务,它扩展了Overlay,但我遇到的问题是,一旦绘制了所有的地图叠加层,地图的缩放或平移速度都非常慢.将所有叠加层添加到地图后,通常会超过2000,但它们都非常小.

考虑到如果覆盖较少,地图将更快地工作,我将所有线条绘制为三个单独的叠加,而不是每条线的单独叠加.这实际上导致了SLOWER平移和缩放地图,所以我又回到了许多小的叠加层.

我将非常感谢一些方法的信息和易于理解的描述,我可以用它来使地图更快.潜在方法的伪代码或真实代码也可以帮助我更好地理解它.我的代码发布在下面.再说一次,请注意我的叠加层和地图显示正确;我想要一种允许更快平移和缩放的方法.

mapOverlay类

public class mapOverlay extends Overlay {private Projection projection;private GeoPoint gp1;private GeoPoint gp2;private int color;public mapOverlay(int color,MapVIEw map,GeoPoint geo1,GeoPoint geo2) {    // super();    this.projection = map.getProjection();    this.gp1 = geo1;    this.gp2 = geo2;    this.color = color;}public voID draw(Canvas canvas,MapVIEw mapv,boolean shadow) {    super.draw(canvas,mapv,false);    Paint mPaint = new Paint();    mPaint.setDither(true);    mPaint.setStyle(Paint.Style.FILL_AND_stroke);    mPaint.setstrokeJoin(Paint.Join.ROUND);    mPaint.setstrokeCap(Paint.Cap.ROUND);    mPaint.setstrokeWIDth(4);    mPaint.setcolor(this.color);    Point p1 = new Point();    Point p2 = new Point();    Path path1 = new Path();    projection.topixels(gp1,p1);    projection.topixels(gp2,p2);    path1.moveto(p1.x,p1.y);    path1.lineto(p2.x,p2.y);    canvas.drawPath(path1,mPaint);}}

mapPopup类

public class mapPopup extends MapActivity {public String[] trailnames;public String tablename = "";public int numTrails = 0;public static GeoPoint[][] geoPoints;public int[] colors = new int[] { color.WHITE,color.BLUE,color.CYAN,color.RED,color.YELLOW,color.magenta,color.GRAY,color.LTGRAY };public int[] newcolors;// public Bitmap b;public GeoPoint firstP;public voID loadMapData(Bitmap b,int[] colors,GeoPoint[][] GPAA,int ZoomLevel) {    // GPAA holds an array of an array of GeoPoint    Log.i("DEBUG","starting loadMapDataTask");    map.setSatellite(true);    MapController mc = map.getController();    mapOverlay[][] mapOverlay = new mapOverlay[GPAA.length][];    Log.i("DEBUG","length of GPAA is: " + GPAA.length);    // i cycles through the first dimension of GPAA    for (int i = 0; i < GPAA.length; i++) {        GeoPoint[] geoPoints = GPAA[i];        int length = geoPoints.length - 1;        mapOverlay[i] = new mapOverlay[length]; //        int pointCount = 0;        // z cycles through the second dimension of GPAA        for (int z = 0; z < length; z++) {            mapOverlay[i][z] = new mapOverlay(colors[i],map,geoPoints[pointCount],geoPoints[pointCount + 1]);            pointCount++;        }    }    // Actually adds overlays to map    List<Overlay> mapOverlays = map.getoverlays();    for (int i = 0; i < mapOverlay.length; i++) {        int length = mapOverlay[i].length;        Log.i("DEBUG","Adding map overlays for trail: " + i);        Log.i("DEBUG","Length of mapOverlay[i] is: " + length);        for (int z = 0; z < length; z++) {            mapOverlays.add(mapOverlay[i][z]);        }    }    mc.animateto(GPAA[0][0]);    mc.setZoom(ZoomLevel);    Rect r = new Rect();    map.getDrawingRect(r);    map.invalIDate(r);}public static class runBGLoad extends        AsyncTask<bgLoadParam,Integer,GeoPoint[][]> {    public GeoPoint[][] geoPoints;    protected GeoPoint[] getGPa(Context context,String name,int ID) {        file file = context.getfileStreamPath(name);        if (file.exists() == false) {            Log.i("DEBUG","Creating file");            inputStream is;            fileOutputStream fos;            try {                Log.i("DEBUG","ID is " + ID);                is = context.getResources().openRawResource(ID);                byte[] buffer = new byte[is.available()];                is.read(buffer);                fos = context.openfileOutput(name,Context.MODE_PRIVATE);                fos.write(buffer);                fos.close();                is.close();            } catch (IOException e) {                e.printstacktrace();            }        } else {            Log.i("DEBUG","file already exists");        }        // Log.i("DEBUG","starting to get geopoints");        List<Location> gpsPoints = XMLParser.getPoints(file);        int i = 0;        int index = 0;        GeoPoint[] geoPoints = new GeoPoint[gpsPoints.size()];        // makes List of gpsPoints into GeoPoint[]        ListIterator<Location> it = gpsPoints.ListIterator();        while (it.hasNext()) {            index = it.nextIndex();            Location loc = gpsPoints.get(index);            geoPoints[i] = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));            it.next();            i++;        }        return geoPoints;    }    @OverrIDe    protected GeoPoint[][] doInBackground(bgLoadParam... params) {        Context context = params[0].getContext();        int tNLength = params[0].getTnames().length;        geoPoints = new GeoPoint[tNLength][];        for (int i = 0; i < params[0].getTnames().length; i++) {            String modname = params[0].getTnames()[i].tolowerCase()                    .replace(' ','_');            int IDentifIEr = context.getResources().getIDentifIEr(modname,"raw",context.getPackagename());            geoPoints[i] = getGPa(params[0].getContext(),modname                    + "_mapfile",IDentifIEr);        }        Log.i("DEBUG","TEST");        mapPopup.geoPoints = geoPoints;        Log.i("DEBUG","TEST2");        return geoPoints;    }    @OverrIDe    protected voID onPostExecute(GeoPoint[][] result) {        Log.i("DEBUG","The points are loaded.");        mapPopup.geoPoints = result;    }}@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    Intent intent = getIntent();    String[] extras = intent.getStringArrayExtra("strings");    tablename = extras[1];    numTrails = Integer.parseInt(extras[2]);    trailnames = intent.getStringArrayExtra("trailnamesA");    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.map_popup_layout);    newcolors = new int[numTrails];    for (int i = 0; i < numTrails; i++) {        newcolors[i] = colors[i];    }    VIEwGroup layout = (VIEwGroup) findVIEwByID(R.ID.map_popup);    TextVIEw[] tVs = new TextVIEw[numTrails];    for (int i = 0; i < numTrails; i++) {        LayoutParams params = new relativeLayout.LayoutParams(                LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);        tVs[i] = new TextVIEw(getApplicationContext());        tVs[i].setText(trailnames[i]);        tVs[i].setID(i + 700);        tVs[i].setTextcolor(colors[i]);        tVs[i].setBackgroundcolor(color.BLACK);        if (i > 0) {            params.addRule(relativeLayout.BELOW,(699 + i));        }        layout.addVIEw(tVs[i],params);    }}@OverrIDepublic voID onWindowFocusChanged(boolean hasFocus) {    super.onWindowFocusChanged(hasFocus);    MapVIEw map = (MapVIEw) findVIEwByID(R.ID.popupMV);    Bitmap b = Bitmap.createBitmap(map.getWIDth(),map.getHeight(),Bitmap.Config.RGB_565);    try {        trailsActivity.mapPreLoad.get();    } catch (InterruptedException e) {        e.printstacktrace();    } catch (ExecutionException e) {        e.printstacktrace();    }    loadMapData(b,newcolors,geoPoints,17);}@OverrIDeprotected boolean isRoutedisplayed() {    // Todo auto-generated method stub    return false;}}
解决方法 我目前面临同样的问题,我刚刚找到了一种解决方法:防止在缩放或平移时绘制叠加层.这并不完美,我仍然在寻找更好的解决方案,但至少可以使用地图而无需等待每个平移或缩放5秒.

这是我在Overlay扩展中使用的代码.它不是Java而是C#(使用MonodroID) – 但它应该很容易理解.

public overrIDe bool OntouchEvent (MotionEvent e,AndroID.GoogleMaps.MapVIEw mapVIEw){    if (e.Action == MotionEventActions.Down)        _mustDraw = false;    else if (e.Action == MotionEventActions.Up)        _mustDraw = true;    return base.OntouchEvent (e,mapVIEw);}public overrIDe voID Draw (AndroID.Graphics.Canvas canvas,AndroID.GoogleMaps.MapVIEw mapVIEw,bool shadow){    if (shadow || !_mustDraw)        return;    // ...}

该解决方案适用于每个基于地图触摸的 *** 作,现在以良好的速度执行,我只是在使用内置缩放控件放大或缩小时缺乏相同行为的实现,但我首先需要对抗我的一些错误之前做这一部分,我稍后会回到这一部分.

@H_404_2@ 总结

以上是内存溢出为你收集整理的android – 添加许多叠加后MapView平移和缩放很慢全部内容,希望文章能够帮你解决android – 添加许多叠加后MapView平移和缩放很慢所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126602.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存