[英]zoom over specific route google map
我有一個隨機緯度和經度點的列表,我正在他們之間繪制一條路線。 我的問題是如何在谷歌地圖中綁定這條路線,我在實用方法下面做了
public static void drawRouteIntoMap(final List<? extends MapHelper> position, final GoogleMap googleMap) {
/*List<MapHelper> position = new ArrayList<MapHelper>();
for (int i = lastPosition; i < maps.size(); i++) {
position.add(maps.get(i));
}*/
if (position.size() > 0 && Validator.isNotNull(googleMap)) {
googleMap.clear();
List<PolylineOptions> polylineOptionses = new ArrayList<PolylineOptions>();
PolylineOptions option = null;
Boolean lastPause = null;
for (MapHelper map : position) {
if (map.isPause()) {
if (Validator.isNull(lastPause) || !lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(255, 0, 155)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
} else {
if (Validator.isNull(lastPause) || lastPause) {
option = new PolylineOptions().width(5).color(Color.rgb(0, 179, 253)).geodesic(true);
polylineOptionses.add(option);
}
option.add(new LatLng(map.getLatitude(), map.getLongitude()));
}
lastPause = map.isPause();
}
for (PolylineOptions options : polylineOptionses) {
googleMap.addPolyline(options);
}
if(Validator.isNotNull(option)){
//List<LatLng> points = option.getPoints();
final LatLngBounds.Builder mapBounds = new LatLngBounds.Builder();
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng startPoint = new LatLng(position.get(0).getLatitude(), position.get(0).getLongitude());
googleMap.addMarker(new MarkerOptions().position(startPoint).title("start").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mapBounds.include(startPoint);
LatLng endPoint = new LatLng(position.get(position.size() - 1).getLatitude(), position.get(position.size() - 1).getLongitude());
mapBounds.include(endPoint);
googleMap.addMarker(new MarkerOptions().position(endPoint).title("finish").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
/* googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.moveCamera(CameraUpdateFactory.zoomOut());*/
}
});
}
}
}
這里最后一次暫停是布爾值,表示它是否是用於指示紅色折線的暫停點。
但它沒有用。感謝任何幫助。
嘗試以這種方式實現
/**
* Zooms a Route (given a List of LalLng) at the greatest possible zoom level.
*
* @param googleMap: instance of GoogleMap
* @param lstLatLngRoute: list of LatLng forming Route
*/
public void zoomRoute(GoogleMap googleMap, List<LatLng> lstLatLngRoute) {
if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
for (LatLng latLngPoint : lstLatLngRoute)
boundsBuilder.include(latLngPoint);
int routePadding = 100;
LatLngBounds latLngBounds = boundsBuilder.build();
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, routePadding));
}
UPDATE
查看圖像后,地圖上方有布局。 所以它需要你設置Variable Padding 。 你可以這樣做
googleMap.setPadding(left, top, right, bottom);
注意:設置變量填充時,可以初始化routePadding = 0
; 在您的情況下,近似值為: left = right = 10
, bottom=100
, top=200
。 一旦設置,您可以根據您的要求校准'。
Recommendation: You can calculate the height of those (top and bottom) layouts in pixels and then set padding accordingly.
為什么放大不起作用的原因可能是因為在調用方法時map尚未膨脹:
moveCamera(com.google.android.gms.maps.CameraUpdate)
嘗試將ViewTreeObserver.OnGlobalLayoutListener
添加到地圖中:
ViewTreeObserver vto = googleMap.getViewTreeObserver();
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
googleMap.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
googleMap.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
}
};
vto.addOnGlobalLayoutListener(globalLayoutListener);
如果上述方法不起作用, GoogleMap
擁有自己的布局偵聽器,您可以使用:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition arg0) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
googleMap.setOnCameraChangeListener(null);
}
});
但是,從play-services-maps 9.4.0版本的API開始,不推薦使用上述方法。 使用以下之一:
您需要將構成折線的所有點都包含在mapBounds
對象中。
此外,您正在使用animateCamera
更新相機,然后使用moveCamera
移動它。 第二次攝像頭更新會覆蓋第一次。 只需刪除該行
googleMap.moveCamera(CameraUpdateFactory.zoomOut());
這條線也是必需的(你不需要移動和動畫相機,只需其中一個):
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(mapBounds.build(), 10));
如果您需要縮小地圖,可以使用CameraUpdateFactory.newLatLngBounds
方法的padding參數進行CameraUpdateFactory.newLatLngBounds
。
我實現了一個類似於@ rishabh-dutt-sharma的解決方案,並且有幾個曲折:
這是代碼(改編自我的原始代碼,我希望沒有拼寫錯誤):
public void zoomRoute(GoogleMap googleMap, List<LatLng> lstLatLngRoute) {
if (googleMap == null || lstLatLngRoute == null || lstLatLngRoute.isEmpty()) return;
LatLngBounds currentLatLongBounds =
googleMap.getProjection().getVisibleRegion().latLngBounds;
boolean updateBounds = false;
for (LatLng latLng : lstLatLngRoute) {
if (!currentLatLongBounds.contains(latLng)) {
updateBounds = true;
}
}
if (updateBounds) {
CameraUpdate cameraUpdate;
if (lstLatLngRoute.size() == 1) {
LatLng latLng = lstLatLngRoute.iterator().next();
cameraUpdate = CameraUpdateFactory.newLatLng(latLng);
} else {
LatLngBounds.Builder builder = LatLngBounds.builder();
for (LatLng latLng : lstLatLngRoute) {
builder.include(latLng);
}
LatLngBounds latLongBounds = builder.build();
cameraUpdate =
CameraUpdateFactory.newLatLngBounds(latLongBounds, MAP_ZOOM_PADDING);
}
try {
googleMap.animateCamera(cameraUpdate, MAP_CAMERA_ANIMATION_DURATION_IN_MILLIS,
new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
});
} catch (IllegalStateException ex) {
// Ignore it. We're just being a bit lazy, as this exception only happens if
// we try to animate the camera before the map has a size
}
}
}
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(endPoint,zoomLevel));
試試它,如果它可以幫助你
你可以做一件事,找出2緯度和經度之間的距離,並檢查你得到的距離。看,它是如何工作的。 這可能對你有用。
googleMap.moveCamera(CameraUpdateFactory.newLatLng(reachLocationLatLng));
if (dist > 2 && dist <= 5) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13.0f));
mapZoomLevel = 12;
} else if (dist > 5 && dist <= 10) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f));
mapZoomLevel = 11;
} else if (dist > 10 && dist <= 20) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(11.0f));
mapZoomLevel = 11;
} else if (dist > 20 && dist <= 40) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(10.0f));
mapZoomLevel = 10;
} else if (dist > 40 && dist < 100) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(9.0f));
mapZoomLevel = 9;
} else if (dist > 100 && dist < 200) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(8.0f));
mapZoomLevel = 8;
} else if (dist > 200 && dist < 400) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(7.0f));
mapZoomLevel = 7;
} else if (dist > 400 && dist < 700) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(6.0f));
mapZoomLevel = 7;
} else if (dist > 700 && dist < 1000) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(5.0f));
mapZoomLevel = 6;
} else if (dist > 1000) {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(4.0f));
mapZoomLevel = 5;
} else {
googleMap.animateCamera(CameraUpdateFactory.zoomTo(14.0f));
mapZoomLevel = 14;
}
謝謝希望這會對你有所幫助。
我是怎么做到的
是的,您可以繪制不在可見地圖上的點。 通過設置LatLngBounds來解決這個問題。 然后顯示LatLngBounds的地圖。 您還可以移動地圖以顯示LatLngBounds。 沒有什么棘手的東西沒有切線或斜坡或地球的雙曲曲率。 所有這些都以度為單位來處理,所以不要過分考慮你的解決方案。 如果你轉動地圖,點將隨之轉動。 因此,將您的點添加到地圖中找到邊界並傾斜相機。 向后傾斜相機,所有點仍然在屏幕上! 這將使您開始使用屏幕上的所有點。
與樣本相同,獲得對谷歌地圖的引用。
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
// mMap = mMapFragment.getMap();
// // Check if we were successful in obtaining the map.
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// use the settings maptype
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
讓我們開始尋找地圖的邊界。
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
A field for LatLngBounds and a LatLngBounds.Builder
private LatLngBounds bounds;
private LatLngBounds.Builder myLatLngBuilder;
initialize the LatLngBounds.Builder
myLatLngBuilder = new LatLngBounds.Builder();
for each point on your map.
LatLng myLatLng = new LatLng(latitude, longitude);
myLatLngBuilder.include(myLatLng);
Then when your finished adding points build your boundary.
bounds = myLatLngBuilder.build();
Now I have the bounds of all the points on my map and I can display just that area of the map. This code I lifted from the map samples.
final View mapView = getSupportFragmentManager().findFragmentById(
R.id.map).getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
// We use the new method when supported
@SuppressLint("NewApi")
// 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);
}
mMap.moveCamera(CameraUpdateFactory
.newLatLngBounds(bounds, 50));
}
});
}
用這種最短的方式回答嘗試是很晚的
首先獲取當前位置latlongs然后獲取目標lat long的寫入代碼,然后使用此代碼計算當前位置和目標位置之間的距離
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
由於距離當前位置95米的GPS數量顯示當前位置引腳或lat長數據,因此您永遠無法獲得相同的lat long數據。 要么你是同一個地方你永遠找不到確切的lat長數據,這必須是不同的點,這樣你需要將當前位置與你的修復lat長數據點保持距離。
當您找到從目標點到當前緯度的“ 距離 ”時,請激活此代碼以設置相機和縮放的動畫。
map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(xxxx,xxxx),21));
對於上面提到的問題,這可能是最簡單的解決方案,對我來說非常有用。
在GoogleMapOptions下添加所有航點到目標:攝像機,然后它將自動設置縮放並使地圖相對於折線(給定路線)居中。
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: this.wayPoints,
zoom: 25,
tilt: 30
}
};
this.map = GoogleMaps.create('map_canvas', mapOptions);
this.map.one(GoogleMapsEvent.MAP_READY)
.then(() => {
console.log('Map is ready!');
this.map.addPolyline({
points:this.wayPoints,
color:'#586bff',
width: 8,
geodesic:true,
});
});
}
航點(this.wayPoints)應采用以下格式:
[{lat: 52.33806, lng: 8.61179},
{lat: 52.33812, lng: 8.61185},
{lat: 52.33812, lng: 8.61186},
{lat: 52.33812, lng: 8.61188}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.