2011-02-18 8 views
11

मैं नक्शा क्षेत्र (केंद्र और अवधि) सेट करने की कोशिश कर रहा हूं ताकि नक्शा एक ही समय में सभी पिन-एनोटेशन दिखाए।आईफोन एमकेमैप व्यू: मानचित्र पर सभी पिन दिखाने के लिए मानचित्र क्षेत्र सेट करें

मुझे एनएसएसटींग से डबल, रेस तक लंबे/लेट निर्देशांक को परिवर्तित करने में समस्या हो रही है। उनके साथ गणना करें।

- (void)updateMemberPins{ 

//calculate new region to show on map 
double center_long = 0.0f; 
double center_lat = 0.0f; 
double max_long = 0.0f; 
double min_long = 0.0f; 
double max_lat = 0.0f; 
double min_lat = 0.0f; 

for (Member *member in members) { 

    //find min and max values 
    if ([member.locLat doubleValue] > max_lat) {max_lat = [member.locLat doubleValue];} 
    if ([member.locLat doubleValue] < min_lat) {min_lat = [member.locLat doubleValue];} 
    if ([member.locLong doubleValue] > max_long) {max_long = [member.locLong doubleValue];} 
    if ([member.locLong doubleValue] < min_long) {min_long = [member.locLong doubleValue];} 

    //sum up long and lang to get average later 
    center_lat = center_lat + [member.locLat doubleValue]; 
    center_long = center_long + [member.locLong doubleValue]; 
} 

//calculate average long/lat 
center_lat = center_lat/[members count]; 
center_long = center_long/[members count]; 

NSLog(@"center long: %d, center lat: %d", center_long, center_lat); 
NSLog(@"max_long: %d, min_long: %d, max_lat: %d, min_lat: %d", max_long, min_long, max_lat, min_lat); 

//create new region and set map 
CLLocationCoordinate2D coord = {latitude: center_lat, longitude: center_long}; 
MKCoordinateSpan span = MKCoordinateSpanMake(abs(max_lat) + abs(min_lat), abs(max_long) + abs(min_long)); 
MKCoordinateRegion region = {coord, span}; 
[resultMapView setRegion:region]; 

//remove all pins from map 
[resultMapView removeAnnotations:resultMapView.annotations]; 

//show member pins 
for (id member in members) { 
    [resultMapView addAnnotation:(Member *) member]; 
} 

}

लॉग-उत्पादन का परिणाम है::

केंद्र लंबे: यहाँ कोड मैं का उपयोग कर रहा है -+१९४६८२७११६, केन्द्र अक्षांश: 1075651472

max_long: -6267216, min_long: 1076018553, max_lat: 0, min_lat: 0

मैं पतला k समस्या (गलत तरीके से) NSString से डबल तक मूल्यों को परिवर्तित करने से आती है, हालांकि मुझे इसे काम करने का कोई तरीका नहीं मिल रहा है ... स्थान-स्ट्रिंग का प्रारूप '43 .5686473 'जैसा है।

कोई संकेत? Cheerz

+0

न्यूनतम अधिकतम लेट लोन के लिए आपके प्रारंभ मूल्य गलत हैं। इन्हें आजमाएं: डबल max_long = -360.0f; डबल min_long = 360.0f; डबल max_lat = -360.0f; डबल min_lat = 360.0f; – igrek

+0

और अवधि गणना? उन्हें क्यों जोड़ें? और यह भी पेट वापस int नहीं देता है इसलिए fabs होना चाहिए: MKCoordinateSpan span = MKCoordinateSpanMake (fabs (max_lat - min_lat), fabs (max_long - min_long)); – igrek

+0

वैसे भी धन्यवाद, +1 ने मुझे कुछ समय बचाया – igrek

उत्तर

5

NSLog (डबल मूल्य दिखाने के लिए), तो आपको %d बदलें इस तरह NSLog() भाग के बजाय %f का उपयोग करना चाहिए,:

NSLog(@"center long: %f, center lat: %f", center_long, center_lat); 
NSLog(@"max_long: %f, min_long: %f, max_lat: %f, min_lat: %f", max_long, min_long, max_lat, min_lat); 

इसके अलावा, MKMapView से क्षेत्र का उपयोग कर अपने स्वयं के बनाने की तुलना में बहुत सरल है। एक बार यह ज़ूम अनुपात के साथ सेट हो जाने के बाद, आपको केवल अलग-अलग निर्देशांक के साथ नक्शे के चारों ओर गतिशील रूप से स्थानांतरित करने की आवश्यकता होती है।

MKCoordinateRegion region = self.mapView.region; 
region.center = centerCoordinate; 
region.span.longitudeDelta /= ratioZoomMax; // Bigger the value, closer the map view 
region.span.latitudeDelta /= ratioZoomMax; 
[self.mapView setRegion:region animated:YES]; // Choose if you want animate or not 
+0

हे पीटर! आपके प्रतिक्रिया के लिए धन्येवाद। आप एनएसएलओजी कथन के साथ सही हैं। हालांकि, यह अनुमान है कि यह मेरे गलत अनुमान नहीं दे रहा है ... अनुपात ज़ूम संस्करण अच्छा है, लेकिन मुझे यह अनुपात नहीं पता है। मेरे पास 'सबसे बाहरी समन्वय' हैं जो मानचित्र पर दिखाई दे रहे हैं ... – pawi

+0

ठीक है, यह पता चला कि मैं गलत संख्याओं से उलझन में था कि गलत लॉग-स्टेटमेंट ने मुझे दिया था। तब मैंने अपने कैल्क्स में कुछ गलतियों को पागल कर दिया - और खराब डेटा (0, 0 निर्देशांक)। – pawi

+0

आप 'अनुपात ज़ूममैक्स' को बहुत व्यापक क्षेत्र दिखाने के लिए छोटे होने के लिए बदल सकते हैं, जो एक दूसरे से दूर पिन दिखाते हैं। अपना कोड दोबारा पढ़ना, मैंने पाया कि आपने केंद्र मूल्य खोजने के लिए अनावश्यक जोड़ा है।जैसा कि आप पहले ही पता लगा चुके हैं, केंद्र ढूंढने के लिए, आपको केवल '(सबसे कम वैल्यू + उच्चतम वैल्यू)/2.0' करना है, इसे' [सदस्यों की गिनती] ' – petershine

22

अंतिम, कुछ समान की तलाश में उन लोगों के लिए काम कर कोड:

- (void)updateMemberPins{ 

    //remove all pins from map 
    [resultMapView removeAnnotations:resultMapView.annotations]; 

    if ([members count] > 0) { 

     @try { 
      //calculate new region to show on map   
      Member *firstMember = [members objectAtIndex:0]; 
      double max_long = [firstMember.locLong doubleValue]; 
      double min_long = [firstMember.locLong doubleValue]; 
      double max_lat = [firstMember.locLat doubleValue]; 
      double min_lat = [firstMember.locLat doubleValue]; 

      //find min and max values 
      for (Member *member in members) { 
       if ([member.locLat doubleValue] > max_lat) {max_lat = [member.locLat doubleValue];} 
       if ([member.locLat doubleValue] < min_lat) {min_lat = [member.locLat doubleValue];} 
       if ([member.locLong doubleValue] > max_long) {max_long = [member.locLong doubleValue];} 
       if ([member.locLong doubleValue] < min_long) {min_long = [member.locLong doubleValue];} 
      } 

      //calculate center of map 
      double center_long = (max_long + min_long)/2; 
      double center_lat = (max_lat + min_lat)/2; 

      //calculate deltas 
      double deltaLat = abs(max_lat - min_lat); 
      double deltaLong = abs(max_long - min_long); 

      //set minimal delta 
      if (deltaLat < 5) {deltaLat = 5;} 
      if (deltaLong < 5) {deltaLong = 5;} 

      //debug 
      //NSLog(@"center long: %f, center lat: %f", center_long, center_lat); 
      //NSLog(@"max_long: %f, min_long: %f, max_lat: %f, min_lat: %f", max_long, min_long, max_lat, min_lat); 

      //create new region and set map 
      CLLocationCoordinate2D coord = {latitude: center_lat, longitude: center_long}; 
      MKCoordinateSpan span = MKCoordinateSpanMake(deltaLat, deltaLong); 
      MKCoordinateRegion region = {coord, span}; 
      [resultMapView setRegion:region]; 


     } 
     @catch (NSException * e) { 
      NSLog(@"Error calculating new map region: %@", e); 
     } 
     @finally { 
      //show member pins 
      for (id member in members) { 
       [resultMapView addAnnotation:(Member *) member]; 
      } 
     } 



    } 

} 
+5

पेट() के बजाय fabs() का उपयोग करके बेहतर हो सकता है। abs() ने मुझे कुछ समस्याएं दीं। –

2
//show member pins 
     for (id member in members) { 
      [resultMapView addAnnotation:(Member *) member]; 
     } 

यह

[resultMapView addAnnotations:members]; 
0

साथ बदला जा सकता आप बस कोड के इस शांति इस्तेमाल कर सकते हैं:

-(void)updateMemberPins 
{ 
    if([members count] == 0) 
     return; 

    double minLat = 90; 
    double minLon = 180; 
    double maxLat = -90; 
    double maxLon = -180; 

    for(Member *member in members) 
    { 
     minLat = fmin(minLat, [member.locLat doubleValue]); 
     minLon = fmin(minLon, [member.locLong doubleValue]); 
     maxLat = fmax(maxLat, [member.locLat doubleValue]); 
     maxLon = fmax(maxLon, [member.locLong doubleValue]); 
    } 

    NSLog(@"MAX LAT: %f, MIN LAT: %f, MAX LONG: %f, MIN LONG: %f,", maxLat, minLat, maxLon, minLon); 

    double midLat = (minLat + maxLat)/2; 
    double midLong = (minLon + maxLon)/2; 

    double deltaLat = abs(maxLat - minLat); 
    double deltaLong = abs(maxLon - minLon); 

    if (deltaLat < 5) {deltaLat = 5;} 
    if (deltaLong < 5) {deltaLong = 5;} 

    //... 
}