我无法缩放起点(绿色标记)和终点(红色标记),我的代码放大了终点。见下图
使用以下方法获取我的第一个和最后一个数据:
public void createMarker(int index, Double latitude, Double longitude, String location, String remarks) {
double latitudeStart, longitudeStart;
double latitudeEnd, longitudeEnd;
index = index + 1;
String text = String.valueOf(index);
BitmapDescriptor image = BitmapDescriptorFactory.fromResource(index);
IconGenerator tc = new IconGenerator(getContext());
if (index == 1) {
image = BitmapDescriptorFactory.fromResource(R.drawable.pin_green);
latitudeStart =latitude;
longitudeStart = longitude;
mMapSnailTrail.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(location)
.snippet(remarks)
.icon(image));
} else if (index == response_last.body().getAsJsonArray().size()) {
image = BitmapDescriptorFactory.fromResource(R.drawable.pin_red);
latitudeEnd =latitude;
longitudeEnd = longitude;
mMapSnailTrail.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(location)
.snippet(remarks)
.icon(image));
} else {
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(latitudeStart,longitudeStart));
builder.include(new LatLng(latitudeEnd,longitudeEnd));
LatLngBounds bounds = builder.build();
mMapSnailTrail.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
mMapSnailTrail.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
marker.hideInfoWindow();
}
});
}
数据纬度和经度来自API,我通过此代码获取它们:
String lati = jo.get("lat").toString();
String latiString = lati;
latiString = latiString.replace("\"", "");
String lat = String.valueOf(latiString);
String longi = jo.get("lng").toString();
String longiString = longi;
longiString = longiString.replace("\"", "");
String lng = String.valueOf(longiString);
最后这是我一直在使用的创建标记:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
//starting point
builder.include(new LatLng(latitude,longitude));
//end point
builder.include(new LatLng(latitude,longitude));
LatLngBounds bounds = builder.build();
mMapSnailTrail.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
我真的需要你的帮助来完成这项任务。我在互联网上搜索,但我找不到解决方案。谢谢你们!
答案 0 :(得分:0)
尝试添加此行代码
map.getUiSettings().setZoomControlsEnabled(true);
答案 1 :(得分:0)
似乎,您使用相同的LatLng
坐标LatLng(latitude,longitude)
进行缩放:
//starting point builder.include(new LatLng(latitude,longitude)); //end point builder.include(new LatLng(latitude,longitude));
可能是
//starting point
builder.include(new LatLng(latitudeStart,longitudeStart));
//end point
builder.include(new LatLng(latitudeEnd,longitudeEnd));
其中(latitudeStart,longitudeStart)
- 起点坐标(绿色标记)和(latitudeEnd,longitudeEnd)
- 终点坐标(红色标记),解决问题。您可以通过这种方式获取latitudeStart
,longitudeStart
,latitudeEnd
和longitudeEnd
(已更新):
// wrong code:
// LatLng latitudeStart, longitudeStart;
// LatLng latitudeEnd, longitudeEnd;
// update2: global variables
double latitudeMin = 1000, longitudeMin = 1000; // better get first value of lat/lon, not 1000
double latitudeMax = -1000, longitudeMax = -1000; // better get first value of lat/lon, not -1000
...
// test current latitude is min or max
if (latitude < latitudeMin) {
latitudeMin = latitude;
} else {
if (latitude > latitudeMax) {
latitudeMax = latitude;
}
}
// test current longitude is min or max
if (longitude< longitudeMin) {
longitudeMin = longitude;
} else {
if (longitude> longitudeMax) {
longitudeMax = longitude;
}
}
if (index == 1) {
image = BitmapDescriptorFactory.fromResource(R.drawable.pin_green);
mMapSnailTrail.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(location)
.snippet(remarks)
.icon(image));
} else if (index == response_last.body().getAsJsonArray().size()) {
image = BitmapDescriptorFactory.fromResource(R.drawable.pin_red);
mMapSnailTrail.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(location)
.snippet(remarks)
.icon(image));
} else {
}
...
然后,在createMarker()
之外:
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(new LatLng(latitudeMin,longitudeMin));
builder.include(new LatLng(latitudeMax,longitudeMax));
LatLngBounds bounds = builder.build();
mMapSnailTrail.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));