2013-02-28 11 views
19

आईओएस के लिए Google मानचित्र एसडीके के साथ समन्वय सरणी के लिए सीमा कैसे फिट करें? मुझे 4 दृश्य मार्करों के लिए मानचित्र ज़ूम करने की आवश्यकता है।आईओएस के लिए Google मानचित्र एसडीके के साथ समन्वय सरणी के लिए सीमा कैसे फिट करें?

+0

मैं यहाँ अपने समान प्रश्न का उत्तर दिया: http://stackoverflow.com/questions/15040409/how-to-setregion-with-google-maps-sdk-for-ios –

उत्तर

56

इस समस्या के लिए मेरा समाधान यहां है। एकाधिक निर्देशांक द्वारा एक GMSCoordinateBounds ऑब्जेक्ट का निर्माण करना।

- (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]]; 
} 

अपडेट किया गया जवाब: चूंकि GMSMapView मार्करों संपत्ति हटाई गई है, आप अपने खुद के सरणी में सभी मार्करों बचाने चाहिए।

अद्यतन तेज 3 जवाब:

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

ठीक काम करता है। हालांकि मार्कर संपत्ति अब बहिष्कृत कर दी गई है। मैंने इसके बजाय एक सरणी में मार्कर स्थानों का उपयोग किया था। –

+0

धन्यवाद, मैंने इस महान समाधान का उपयोग किया :) लेकिन जैसा कि @ManishAhuja ने कहा, मैंने सीमाएं बनाने के लिए अपने स्वयं के संग्रहीत GMSMarker सरणी का उपयोग किया। –

+0

धन्यवाद @ManishAhuja और औबादाटाइजो। मैंने जवाब अपडेट किया है। – Lirik

3

समय के लिए, Google ने आखिरकार जीएमएस कॉर्डिनेट बाउंड्स लागू किया है, आप इसका उपयोग GMSCameraUpdate के साथ कर सकते हैं।

विवरण के लिए, कृपया आधिकारिक reference देखें।

5

स्विफ्ट 3.0 Lirik के जवाब के संस्करण:

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

और यहाँ अपने तरीके से बताया गया है:

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

और आप कॉल कर सकते हैं मेरे उपर्युक्त कार्य:

self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])

संबंधित मुद्दे