gpt4 book ai didi

android - 缩放以适合 map 上的所有标记谷歌地图 v2

转载 作者:IT老高 更新时间:2023-10-28 23:37:37 26 4
gpt4 key购买 nike

我想设置 map 缩放级别以适合 map 上的所有标记。正如很多人所说,我使用了以下方法,但它对我不起作用。它正在显示其他点。

if (positions.size() > 0) {

final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker m : positions) {
builder.include(m.getPosition());
}
builder.include(positions.get(i).getPosition());
}

try {
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

@Override
public void onCameraChange(CameraPosition arg0) {

googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(), UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);

}
});

} catch (IllegalStateException e) {
// TODO: handle exception
final View mapView = getSupportFragmentManager()
.findFragmentById(R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {

// We check which build version we are using.
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver()
.removeGlobalOnLayoutListener(
this);
} else {
mapView.getViewTreeObserver()
.removeOnGlobalLayoutListener(
this);
}

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {

@Override
public void onCameraChange(CameraPosition arg0) {

googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(
builder.build(),
UserDataManager.getInstance().getWidth(),
UserDataManager.getInstance().getWidth(),0));
googleMap.setOnCameraChangeListener(null);

}
});

}
});
}
}
}

最佳答案

试试这个

//Calculate the markers to get their position
LatLngBounds.Builder b = new LatLngBounds.Builder();
for (Marker m : markers) {
b.include(m.getPosition());
}
LatLngBounds bounds = b.build();
//Change the padding as per needed
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 25,25,5);
mMap.animateCamera(cu);

查看 Google Maps v2 library我创建的包含此功能的只需使用

Marker m1,m2, m3, m4 .... ;

mapHelper.zoomToFitMarkers(m1,m2, m3, m4 ....);

编辑:

获取当前位置

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//You can use GPS_PROVIDER also
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
//Add your marker here and zoom to fit it
//mapHelper.zoomToFitMarkers(m1,m2, m3, m4,mapHelper.addMarker(currentLatitude,currentLongitude,"Current Location"););
}
});

关于android - 缩放以适合 map 上的所有标记谷歌地图 v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16416041/

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