我正在尝试使用Google Maps API v2在2点之间绘制折线。它在Android KitKat和所有版本低于此工作正常。但是当在具有高于KitKat的Android版本的手机中编译项目时,未显示折线,即路线未显示。我能知道为什么会这样吗? 这是我的代码
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
ArrayList<LatLng> points = null;
//PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();
String distance = "";
String duration = "";
if (result.size() < 1) {
Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show();
return;
}
points = new ArrayList<LatLng>();
// Traversing through all the routes
for (int i = 0; i < result.size(); i++) {
//points = new ArrayList<LatLng>();
// lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
if (j == 0) { // Get distance from the list
distance = (String) point.get("distance");
continue;
} else if (j == 1) { // Get duration from the list
duration = (String) point.get("duration");
continue;
}
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
}
Toast.makeText(getBaseContext(), distance, Toast.LENGTH_LONG).show();
tvDistanceDuration.setText(distance);
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(8);
lineOptions.color(Color.BLUE);
lineOptions.geodesic(true);
// Drawing polyline in the Google Map for the i-th route
line = mMap.addPolyline(lineOptions);
}
我发现只有1个与此问题相似的链接。
google map polyline is not working on android version 5.0(lollipop)
我根据那个答案改变了我的代码。但那也不成功。关于某些东西如何在较低的API版本中工作而不是在较高的API版本中,它非常令人困惑。