2013-02-28 28 views

Trả lời

56

Đâ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) 
    } 
+2

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. –

+0

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. –

+0

Cảm ơn @ManishAhuja và AubadaTaijo. Iv'e đã ​​cập nhật câu trả lời. – Lirik

3

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.

5

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])

Các vấn đề liên quan