gpt4 book ai didi

android - 如何在 Android 中缓冲多段线或围绕多段线绘制多边形?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:41 26 4
gpt4 key购买 nike

我正在尝试在 MapsV2 for android 中的多段线周围放置一个缓冲区,但我还没有找到如何做,我正在考虑在多段线周围绘制多边形,但还没有找到这样做的人,这可能吗?

最佳答案

你想在 map 上画路径还是在 map 上画直线

在这两种情况下,您都可以遵循以下代码

PolylineOptions polyLineOptions = new PolylineOptions();
ArrayList<LatLng> points = new ArrayList<>();

//Code to populate Latlng

polyLineOptions.addAll(points);
polyLineOptions.width(mActivity.getResources().getDimensionPixelSize(R.dimen._2sdp));
polyLineOptions.color(ContextCompat.getColor(mContext, Color_Resource_id));
if (polyLineOptions != null) {
googleMap.addPolyline(polyLineOptions);
}

以上代码是在 map 上只画直线。

现在如果你想在 map 上绘制完整的路径,你可能需要使用 google direction api https://maps.googleapis.com/maps/api/directions/

您可以使用下面的类来解析相同的响应以获得总的 latlngs

public class PathJSONParser {

public List<List<HashMap<String, String>>> parse(JSONObject jObject) {
List<List<HashMap<String, String>>> routes = new ArrayList<>();
JSONArray jRoutes;
JSONArray jLegs;
JSONArray jSteps;
try {
jRoutes = jObject.getJSONArray("routes");
/** Traversing all routes */
for (int i = 0; i < jRoutes.length(); i++) {
jLegs = ((JSONObject) jRoutes.get(i)).getJSONArray("legs");
List<HashMap<String, String>> path = new ArrayList<>();

/** Traversing all legs */
for (int j = 0; j < jLegs.length(); j++) {
jSteps = ((JSONObject) jLegs.get(j)).getJSONArray("steps");

/** Traversing all steps */
for (int k = 0; k < jSteps.length(); k++) {
String polyline;
polyline = (String) ((JSONObject) ((JSONObject) jSteps
.get(k)).get("polyline")).get("points");
List<LatLng> list = decodePoly(polyline);

/** Traversing all points */
for (int l = 0; l < list.size(); l++) {
HashMap<String, String> hm = new HashMap<>();
hm.put("lat",
Double.toString(( list.get(l)).latitude));
hm.put("lng",
Double.toString(( list.get(l)).longitude));
path.add(hm);
}
}
routes.add(path);
}
}

} catch (Exception e) {
e.printStackTrace();
}
return routes;
}

/**
* Method Courtesy :
* jeffreysambells.com/2010/05/27
* /decoding-polylines-from-google-maps-direction-api-with-java
* */
private List<LatLng> decodePoly(String encoded) {

List<LatLng> poly = new ArrayList<>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;

while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;

shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;

LatLng p = new LatLng((((double) lat / 1E5)),
(((double) lng / 1E5)));
poly.add(p);
}
return poly;
}
}

最后你可以用下面的代码在你的类中使用它

private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

@Override
protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

JSONObject jObject;
List<List<HashMap<String, String>>> routes = new ArrayList<>();

try {
jObject = new JSONObject(jsonData[0]);
PathJSONParser parser = new PathJSONParser();
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}

return routes;
}

@Override
protected void onPostExecute(List<List<HashMap<String, String>>> routes) {
if (routes != null && routes.size() > 0) {
new DrawPoly().execute(routes);
}

}
}

private class DrawPoly extends AsyncTask<List<List<HashMap<String, String>>>,
PolylineOptions, PolylineOptions> {

@Override
protected PolylineOptions doInBackground(List<List<HashMap<String, String>>>... params) {
PolylineOptions polyLineOptions = new PolylineOptions();
ArrayList<LatLng> points;

if (params[0] != null && params[0].size() > 0) {
// traversing through routes
for (int j = 0; j < params[0].size(); j++) {
points = new ArrayList<>();
polyLineOptions = new PolylineOptions();
try {
List<HashMap<String, String>> path = params[0].get(j);
for (int k = 0; k < path.size(); k++) {
HashMap<String, String> point = path.get(k);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
} catch (Exception e) {
e.printStackTrace();
}
polyLineOptions.addAll(points);
polyLineOptions.width(mActivity.getResources().getDimensionPixelSize(R.dimen.five));
polyLineOptions.color(ContextCompat.getColor(mContext, R.color.red));
}
}
return polyLineOptions;
}

@Override
protected void onPostExecute(PolylineOptions polyLineOptions) {
super.onPostExecute(polyLineOptions);
if (polyLineOptions != null) {
googleMap.addPolyline(polyLineOptions);
}
}
}

您需要确保将其放入 asyncTask 中。我希望它对你有用。

关于android - 如何在 Android 中缓冲多段线或围绕多段线绘制多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454857/

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