所以,我正在Android App上的Google Map上显示从A点到B点的路线。这一切都很好。我现在正在做的是通过这样做缩放以适应地图上的两个标记:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng marker : markerPoints) {
builder.include(marker);
}
LatLngBounds bounds = builder.build();
int padding = 20; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
padding);
map.moveCamera(cu);
map.animateCamera(cu);
问题是这不包括多边形线,因此如果路线走出屏幕,它会切断多边形线,如下所示:
关于如何缩放以适应多边形线的任何想法?谢谢!
答案 0 :(得分:3)
我明白了。我只是跟踪Poly Line中的每个LatLng并将它们添加到LatLng构建器
private class ParserTask extends
AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
// Parsing the data in non-ui thread
@Override
protected List<List<HashMap<String, String>>> doInBackground(
String... jsonData) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(jsonData[0]);
DirectionsJSONParser parser = new DirectionsJSONParser();
// Starts parsing data
routes = parser.parse(jObject);
} catch (Exception e) {
e.printStackTrace();
}
return routes;
}
// Executes in UI thread, after the parsing process
@Override
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;
}
// 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);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(8);
lineOptions.color(Color.BLUE);
}
tvDistanceDuration.setText("Distance:" + distance + ", Duration:"
+ duration);
// Drawing polyline in the Google Map for the i-th route
map.addPolyline(lineOptions);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
/*
* for (Marker marker : markers) {
* builder.include(marker.getPosition()); }
*/
for (LatLng point : points) {
builder.include(point);
}
LatLngBounds bounds = builder.build();
int padding = 20; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
padding);
map.moveCamera(cu);
map.animateCamera(cu, 2000, null);
}
}
答案 1 :(得分:2)
我会说,在构建折线时,您会跟踪线条的范围。然后当你缩放时使用那些范围而不是点。
答案 2 :(得分:1)
我尝试了这段代码,它的功能对我很有用,而且我在这里附加了输出屏幕。
LatLng from_Latlng=new LatLng(11.9464816,79.8091988);
LatLng to_Latlong=new LatLng(12.9876327,80.1260089);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(from_Latlng);
builder.include(to_Latlong);
LatLngBounds bounds = builder.build();
GoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100), 2000, null);
答案 3 :(得分:1)
我是从Premkumar的复制过来的。如果您获得2分以上,并且您想确保一切都适合(也许中间有2分,而且周围有很多东西在转)。该解决方案的工作方式是创建一个minLatLng和maxLatLng,然后使用Premkumar的答案中的newLatLngBounds解决方案:
var minLat: Double? = null
var minLng: Double? = null
var maxLat: Double? = null
var maxLng: Double? = null
allLocs!!.forEach {
if (minLat == null) {
minLat = it.latitude
maxLat = it.latitude
minLng = it.longitude
maxLng = it.longitude
} else {
minLat = min(it.latitude, minLat!!)
maxLat = max(it.latitude, maxLat!!)
minLng = min(it.longitude, minLng!!)
maxLng = max(it.longitude, maxLng!!)
}
}
val builder = LatLngBounds.builder()
builder.include(LatLng(minLat!!, minLng!!))
builder.include(LatLng(maxLat!!, maxLng!!))
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100))
很明显,如果您实时需要此信息,那么每次获得新位置时记录下自己的最大和最小值将更为有效。