1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| - (void)loadLocation { MyPlace *place1 = [[MyPlace alloc] initWithJSON:[NSDictionary dictionaryWithObjectsAndKeys: @"The place 1", @"name", @"3.12345", @"lat", @"101.43219", @"lng", nil]]; MyPlace *place2 = [[MyPlace alloc] initWithJSON:[NSDictionary dictionaryWithObjectsAndKeys: @"The place 2", @"name", @"4.98721", @"lat", @"101.82665", @"lng", nil]]; MyPlace *place3 = [[MyPlace alloc] initWithJSON:[NSDictionary dictionaryWithObjectsAndKeys: @"The place 1", @"name", @"5.88621", @"lat", @"100.99811", @"lng", nil]];
NSArray *places = @[place1, place2, place3];
MKCoordinateRegion region = [self regionForAnnotations:places]; [self.mapView setRegion:region animated:YES];
[self.mapView addAnnotations:places]; }
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MyPlace class]]) { static NSString *identifier = @"MyLocation"; MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
UIButton *rightButton; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; annotationView.enabled = YES; annotationView.canShowCallout = YES; annotationView.animatesDrop = NO; annotationView.pinColor = MKPinAnnotationColorGreen; annotationView.image = [UIImage imageNamed:@"IconMarker"];
annotationView.animatesDrop = YES; annotationView.draggable = YES; rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [rightButton addTarget:self action:@selector(placeTouched:) forControlEvents:UIControlEventTouchUpInside]; annotationView.rightCalloutAccessoryView = rightButton; } else { annotationView.annotation = annotation; } rightButton.tag = 4000 + ((MyPlace *)annotation).theId; return annotationView; } return nil; }
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState { if (newState == MKAnnotationViewDragStateEnding) { CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate; NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude); } }
- (MKCoordinateRegion)regionForAnnotations:(NSArray *)annotations { MKCoordinateRegion region; if ([annotations count] == 0) { region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate, 1000, 1000); } else if ([annotations count] == 1) { id <MKAnnotation> annotation = [annotations lastObject]; region = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 1000, 1000); } else { CLLocationCoordinate2D topLeftCoord; topLeftCoord.latitude = -90; topLeftCoord.longitude = 180; CLLocationCoordinate2D bottomRightCoord; bottomRightCoord.latitude = 90; bottomRightCoord.longitude = -180; for (id <MKAnnotation> annotation in annotations) { topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); } const double extraSpace = 1.1; region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) / 2.0; region.center.longitude = topLeftCoord.longitude - (topLeftCoord.longitude - bottomRightCoord.longitude) / 2.0; region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * extraSpace; region.span.longitudeDelta = fabs(topLeftCoord.longitude - bottomRightCoord.longitude) * extraSpace; } return [self.mapView regionThatFits:region]; } ...
|