2011-03-29 9 views
6

मैं यह निर्धारित करना चाहता हूं कि क्या उपयोगकर्ता ने मानचित्र के एक निश्चित प्रतिशत से अधिक स्क्रॉल किया है, फिर उपयोगकर्ता स्थान से मानचित्र के केंद्र को अक्षम करना (मैप्स ऐप कैसे काम करता है)।यह पता लगाना कि कोई उपयोगकर्ता MKMapView को एक निश्चित दूरी पर स्क्रॉल करता है?

मुझे यकीन नहीं है कि किस तरीके का उपयोग करना है।

मुझे लगता है कि यह एक आयत बना सकते हैं और देखें कि आयत वर्तमान केंद्र बिंदु शामिल करने के लिए सरल हो सकता है, लेकिन मैं आईओएस 3 लक्षित करने के लिए है, तो मैं नए MapKit APIs को का उपयोग नहीं कर सकते हैं।

मैं CLLocation साथ futzing किया है की कोशिश की, और distanceFrom का उपयोग कर, वर्तमान mapcenter, और उपयोगकर्ताओं को स्थान के बीच है, लेकिन मैं यह पता लगाने की है कि दूरी एक निश्चित प्रतिशत है कोशिश कर रहा हूँ।

उत्तर

15

मैं व्यक्तिगत रूप से जब किसी के बारे में कैसे एक इस बारे में जा सकते हैं कोड सामान्य गद्य बनाम का एक टुकड़ा पोस्ट कर सकते हैं इसे और अधिक उपयोगी पाते हैं।

एक हेडर फाइल में मेरे पास है:

#define SCROLL_UPDATE_DISTANCE   80.00 

और मेरे विचार में (जो है दोनों एक CLLocationManagerDelegate, MKMapViewDelegate के लिए प्रतिनिधि) यहाँ मैं क्या आया हूँ- मोटे तौर पर बस बेहतर इस सवाल का जवाब के लिए बाहर काट दिया है :

// this method is called when the map region changes as a delegate of MKMapViewDelegate 
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    NSLog(@"regionDidChangeAnimated"); 
    MKCoordinateRegion mapRegion; 
    // set the center of the map region to the now updated map view center 
    mapRegion.center = mapView.centerCoordinate; 

    mapRegion.span.latitudeDelta = 0.3; // you likely don't need these... just kinda hacked this out 
    mapRegion.span.longitudeDelta = 0.3; 

    // get the lat & lng of the map region 
    double lat = mapRegion.center.latitude; 
    double lng = mapRegion.center.longitude; 

    // note: I have a variable I have saved called lastLocationCoordinate. It is of type 
    // CLLocationCoordinate2D and I initially set it in the didUpdateUserLocation 
    // delegate method. I also update it again when this function is called 
    // so I always have the last mapRegion center point to compare the present one with 
    CLLocation *before = [[CLLocation alloc] initWithLatitude:lastLocationCoordinate.latitude longitude:lastLocationCoordinate.longitude]; 
    CLLocation *now = [[CLLocation alloc] initWithLatitude:lat longitude:lng]; 

    CLLocationDistance distance = ([before distanceFromLocation:now]) * 0.000621371192; 
    [before release]; 
    [now release]; 

    NSLog(@"Scrolled distance: %@", [NSString stringWithFormat:@"%.02f", distance]); 

    if(distance > SCROLL_UPDATE_DISTANCE) 
    { 
     // do something awesome 
    } 

    // resave the last location center for the next map move event 
    lastLocationCoordinate.latitude = mapRegion.center.latitude; 
    lastLocationCoordinate.longitude = mapRegion.center.longitude; 

} 

आशा है कि आपको सही दिशा में भेजता है।

दूरी फ्रॉमोकेशन आईओएस 3.2 और बाद में है। initWithLatitude आईओएस 2.0 और बाद में है। एमके कॉर्डिनेट रीजन आईओएस 3.0 और बाद में है। एमकेमैप व्यू सेंटर समन्वयक आईओएस 3.0 और बाद में है।

इसके अलावा- कृपया कूदने के लिए स्वतंत्र महसूस करें और मुझे सीधे सेट करें जहां मैंने मिटा दिया है। मैं यह सब खुद को समझ रहा हूं- लेकिन यह अब तक मेरे लिए काफी अच्छा काम कर रहा है।

उम्मीद है कि यह किसी की मदद करेगा।

+0

आप सही हैं। मुझे जवाब पोस्ट करना चाहिए था।यदि आप समझाते हैं कि आप निरंतर उपयोग क्यों करते हैं: 0.000621 ... आपके उत्तर में, मैं इसे स्वीकृत उत्तर के रूप में चिह्नित करूंगा। – Alan

+0

मील से मील रूपांतरण (हालांकि एक मैला वाला एक और किसी प्रकार का स्थिर होना चाहिए)। –

2

पहला सबक: SO पर देर रात प्रश्न पूछें नहीं।

दूसरा सबक: आप इसे केवल उपयोगकर्ता के वर्तमान स्थान से एक CGPoint, और MapView केंद्र से एक CGPoint निर्माण करके प्राप्त कर सकते हैं।

दो बिंदुओं के साथ, बस दूरी की गणना करें, और देखें कि यह एक निश्चित दहलीज से पहले है या नहीं।

आप मानचित्र केंद्र के चारों ओर एक सीजीईजीईटी भी बना सकते हैं, और यदि यह आसान है तो CGRectContainsPoint की जांच करें।

- (BOOL) isUserPointInsideMapCenterRegion 
{ 
    CLLocation * ul = _mapView.userLocation.location; 

    CGPoint userPoint = [_mapView convertCoordinate: ul.coordinate toPointToView: _mapView]; 
    CGPoint mapPoint = [_mapView convertCoordinate: _mapView.centerCoordinate toPointToView: _mapView]; 

    if (fabs(userPoint.x - mapPoint.x) > MAP_CENTER_RECTANGLE_SIZE || fabs(userPoint.y - mapPoint.y) > MAP_CENTER_RECTANGLE_SIZE) 
    { 
     return NO; 
    } 

    return YES; 
} 
+1

मैं जानता हूँ कि इस पुरानी है, लेकिन इसके मैं एकदम बिंदास कोड के बाद वास्तव में क्या था! –

+0

खुशी हुई यह मदद की। – Alan

2

मुझे एहसास है कि यह प्रश्न अब थोड़ा पुराना है, लेकिन मुझे लगता है कि this other question में वर्णित उत्तर अधिक मजबूत है क्योंकि किसी भी कारण से प्रतिनिधि विधि को निकाल दिया जा सकता है। स्क्रॉल का पता लगाने के लिए UIPanGestureRecognizer का उपयोग करना मतलब है कि उपयोगकर्ता ने मैन्युअल रूप से मानचित्र को स्क्रॉल किया है, और यह जांच सकता है कि मानचित्र पर मीटर पर निर्भर होने के बजाय मानचित्र ने एक्स पिक्सेल स्क्रॉल किया है या नहीं, जिसका अर्थ है कि उपयोगकर्ता ज़ूम स्तर के आधार पर अधिक या कम स्क्रॉल कर चुका है।

https://stackoverflow.com/a/11675587/159758

+0

धन्यवाद उस सवाल को इंगित करने के लिए डोनाली। – Alan