gpt4 book ai didi

android - 在 asynctask android 中添加折线

转载 作者:行者123 更新时间:2023-11-30 01:51:20 24 4
gpt4 key购买 nike

我在添加折线时遇到了性能问题,我认为也许可以将它们添加到扩展 AsyncTask 的单独类中。然而,据我所知,不能以这种方式添加 UI 元素(多段线是 UI 元素)。

为什么我在绘制折线时遇到性能问题?好吧,我的折线不是从 pos A 到 pos B 绘制的,而是从我当前位置到目的地绘制的(为了应用程序 atm 而硬编码)。因此,在执行 onLocationChange 监听器时会绘制多段线,因此我的应用程序需要大量的处理能力。

对于如何在这种情况下使用 AsyncTask 有什么想法吗?

这是主类:

 mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

@Override
public void onMyLocationChange(Location arg0) {
// Get positions!
currentPOS = new LatLng(arg0.getLatitude(), arg0.getLongitude());
LatLng dst = new LatLng(58.378249, 26.714673);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(currentPOS, 13);
mMap.animateCamera(yourLocation);
mMap.addMarker(new MarkerOptions().position(dst).title("SCHOOL!"));
/*
// Remove comments to add marker to Liivi 2!
mMap.addMaker(new MarkerOptions().position(currentPOS).title("My POS"));
*/
if (currentPOS != null) {
//This is supposed to show directions
DirectionAPI directionAPI = new DirectionAPI(currentPOS, dst);
GoogleResponse googleResponse = null;
try {
googleResponse = (GoogleResponse) directionAPI.execute().get();
} catch (InterruptedException e) {
Log.e("CATCH","INTERRUPDED");
e.printStackTrace();
} catch (ExecutionException e) {
Log.e("CATCH","EXECUTIONEXCEPTION");
e.printStackTrace();
}
if (googleResponse.isOk()){
DrivingDirection drivingDirection = new DrivingDirection(googleResponse.getJsonObject());
polyline = drivingDirection.getTotalPolyline();
new drawPath(mMap,polyline).execute();
}
}
}
});

这是路径绘制的Async(会因UI冲突而报错):

import android.graphics.Color;
import android.os.AsyncTask;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.ArrayList;

/**
* Created by Kevin on 7.10.2015.
*/
public class drawPath extends AsyncTask{

private static ArrayList<LatLng> polyline;
private static GoogleMap mMap;

public drawPath(GoogleMap map, ArrayList<LatLng> polyline){
this.mMap = map;
this.polyline = polyline;
}

@Override
protected Object doInBackground(Object[] params) {
for (int i = 0; i < polyline.size() - 1; i++) {
LatLng src = polyline.get(i);
LatLng dest = polyline.get(i + 1);

// mMap is the Map Object
Polyline line = mMap.addPolyline(
new PolylineOptions().add(
new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude,dest.longitude)
).width(2).color(Color.BLUE).geodesic(true)
);
}

return null;
}
}

最佳答案

我解决这个问题的方式不是单独添加每条多段线 map ,而是整条多段线。例如,在我的位置距目的地约 4 公里之前,它之间有 280 条折线。在每个 onLocationChange 上,这些多段线都被一条一条地绘制到 map 上。现在它们全部一次性添加 - AsyncTask 将在后台创建折线,并在执行后添加它们。

@Override
protected Object doInBackground(Object[] params) {
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int z = 0; z < polyline.size(); z++) {
LatLng point = polyline.get(z);
options.add(point);
}
return options;
}

protected void onPostExecute(Object result) {
Polyline line = mMap.addPolyline((PolylineOptions) result);
}

关于android - 在 asynctask android 中添加折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com