Làm thế nào để phù hợp với giới hạn cho mảng phối hợp với google maps sdk for iOS? Tôi cần thu phóng bản đồ cho 4 điểm đánh dấu hiển thị.Làm thế nào để phù hợp với giới hạn cho mảng phối hợp với google maps sdk for iOS?
Trả lời
Đây là giải pháp của tôi cho vấn đề này. Xây dựng đối tượng GMSCoordinateBounds theo nhiều tọa độ.
- (void)focusMapToShowAllMarkers
{
CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];
for (GMSMarker *marker in _markers)
bounds = [bounds includingCoordinate:marker.position];
[_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]];
}
cập nhật câu trả lời: Kể từ GMSMapView dấu bất động sản bị phản đối, bạn nên lưu tất cả các dấu hiệu trong mảng riêng bạn.
được cập nhật nhanh chóng 3 câu trả lời:
func focusMapToShowAllMarkers() {
let firstLocation = (markers.first as GMSMarker).position
var bounds = GMSCoordinateBoundsWithCoordinate(firstLocation, coordinate: firstLocation)
for marker in markers {
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fitBounds(bounds, withPadding: CGFloat(15))
self.mapView.animate(cameraUpdate: update)
}
Hoạt động tốt. Mặc dù thuộc tính điểm đánh dấu đã không còn được dùng nữa. Thay vào đó, tôi đã sử dụng các vị trí điểm đánh dấu mà tôi đã có trong một mảng. –
Cảm ơn bạn, tôi đã sử dụng giải pháp tuyệt vời này :) nhưng như @ManishAhuja đã nói, tôi đã sử dụng mảng GMSMarker được lưu trữ của riêng mình để xây dựng các giới hạn. –
Cảm ơn @ManishAhuja và AubadaTaijo. Iv'e đã cập nhật câu trả lời. – Lirik
Hiện tại, Google cuối cùng đã triển khai GMSCoordinateBounds, bạn có thể sử dụng nó với GMSCameraUpdate.
Để biết chi tiết, vui lòng kiểm tra chính thức reference.
Swift 3.0 phiên bản của Lirik của câu trả lời:
func focusMapToShowAllMarkers() {
let myLocation: CLLocationCoordinate2D = self.markers.first!.position
var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation)
for marker in self.markers {
bounds = bounds.includingCoordinate(marker.position)
self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
}
}
Và đây là cách của riêng tôi:
func focusMapToShowMarkers(markers: [GMSMarker]) {
guard let currentUserLocation = self.locationManager.location?.coordinate else {
return
}
var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation,
coordinate: currentUserLocation)
_ = markers.map {
bounds = bounds.includingCoordinate($0.position)
self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
}
}
Và bạn có thể gọi tôi ở trên như sau:
self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])
Tôi đã trả lời câu hỏi tương tự của bạn ở đây: http://stackoverflow.com/questions/15040409/how-to-setregion-with-google-maps-sdk-for-ios –