2015-02-10 9 views
5

मैं उपयोगकर्ता का स्थान और आसपास के क्षेत्र को प्रदर्शित करना चाहता हूं, लेकिन मैं उपयोगकर्ता को क्षेत्र के चारों ओर घूमने की अनुमति देना चाहता हूं। अभी अगर मैं मानचित्र पर कहीं और स्क्रॉल करने का प्रयास करता हूं तो यह मुझे स्वचालित रूप से केंद्र में उपयोगकर्ता के साथ बेस क्षेत्र में ले जाता है। मैं इसे कैसे रोकूं? मैं केंद्र में उपयोगकर्ता के साथ प्रारंभिक दृश्य दिखाना चाहता हूं, लेकिन मैं भी चारों ओर स्क्रॉल करने में सक्षम होना चाहता हूं। अग्रिम धन्यवाद आप लोग बहुत मददगार हैं!स्विफ्ट मैपकिट - उपयोगकर्ता स्थान से दूर जाएं

आयात UIKit आयात MapKit आयात CoreLocation

वर्ग ViewControllerMain: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView! 

var locationManager:CLLocationManager! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    locationManager = CLLocationManager() 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 
    locationManager.startUpdatingLocation() 
    mapView.showsUserLocation = true 
    mapView.delegate = self 

    let longPress = UILongPressGestureRecognizer(target: self, action: "action:") 
    longPress.minimumPressDuration = 1.0 
    mapView.addGestureRecognizer(longPress) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    let regionToZoom = MKCoordinateRegionMake(manager.location.coordinate, MKCoordinateSpanMake(0.01, 0.01)) 
    mapView.setRegion(regionToZoom, animated: true) 
} 

उत्तर

4

didUpdateLocations में आपका कोड क्षेत्र को रीसेट कर रहा है। आपके पास दो विकल्प हैं।

  1. एक ivar में स्टोर करें कि आपने पहले स्थान को पहले ही सेट कर दिया है या नहीं। केवल अगर आपने नहीं किया है तो क्षेत्र को सेट करें।

  2. एक टाइमर सेट करें जो 15 सेकंड के लिए चलता है। यदि नक्शा उपयोगकर्ता द्वारा स्थानांतरित किया जाता है तो आप टाइमर को रीसेट करते हैं। जब टाइमर समाप्त हो जाता है तो आप उपयोगकर्ता के स्थान पर हाल ही में कर सकते हैं।

यह मानचित्र को उपयोगकर्ता के चारों ओर केंद्रित रखेगा लेकिन कुछ संदर्भ प्राप्त करने के लिए उन्हें थोड़ा सा पैन करने में सक्षम बनाएगा।

This answer shows how to do it in Objective-C

+0

आप या तो विकल्प का एक उदाहरण प्रदान कर सकते हैं:

यहाँ एक उदाहरण के रूप में मेरा पूरा कोड है? मैं अवधारणा को समझता हूं। संवैधानिक रूप से मैं कुछ गलत तरीके से कर रहा हूँ – Murph

2
locationManager.stopUpdatingLocation(); 
इस

तुम सब, locationManager मज़ा के अंत में की जरूरत है अगर आप अभी भी दोनों स्विफ्ट और Obj सी में

0

This thread had a good example लिए देख रहे हैं पर टिप्पणी के लिए देखने के लिए सुनिश्चित करें है अगर आप स्विफ्ट का उपयोग करते हैं तो मैंने जवाब दिया है।

सेट अप करने के बाद, didUpdateLocations के भीतर नियंत्रण प्रवाह का उपयोग करें, ताकि उपयोगकर्ता के स्थान को फिर से केंद्रित कर सकें, यदि उपयोगकर्ता ने मानचित्र को छुआ नहीं है।

@IBOutlet weak var theMap: MKMapView! 

// ... 


// This var and the three following functions are used to tell if the map moves because of the user. 
// This is used in the control flow in didUpdateLocations 

private var mapChangedFromUserInteraction = false 


private func mapViewRegionDidChangeFromUserInteraction() -> Bool { 
    let view: UIView = self.theMap.subviews[0] as UIView 
    // Look through gesture recognizers to determine whether this region change is from user interaction 
    if let gestureRecognizers = view.gestureRecognizers { 
     for recognizer in gestureRecognizers { 
      if(recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended) { 
       return true 
      } 
     } 
    } 
    return false 
} 

func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) { 
    mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction() 
    if (mapChangedFromUserInteraction) { 
     // user changed map region 
     println("user changed map region") 


    } 
} 

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { 
    if (mapChangedFromUserInteraction) { 
     // user changed map region 

     println("user changed map region") 


    } 
} 

// This function is called each time the user moves. 
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 




// Use Control Flow: if the user has moved the map, then don't re-center. 
// NOTE: this is using 'mapChangedFromUserInteraction' from above. 

    if mapChangedFromUserInteraction == true { 

     // do nothing, because the user has moved the map. 

    } 

    else { 

     // update on location to re-center on the user. 

     // set X and Y distances for the span (zoom). This is very zoomed in. 
     let spanX = 0.0005 
     let spanY = 0.0005 

     // Create a region using the user's location, and the zoo. 
     var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) 

     // set the map to the new region 
     theMap.setRegion(newRegion, animated: true) 

     } 

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