1

enter image description here

I would like to drop a pin on the map showing the location of each address. The labels are pre-filled with data, so I am assuming the annotation code will need to go in the viewDidLoad.

To avoid any confusion and to help clarify a correct answer, the labels are named lblLeftAddress and lblRightAddress.

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
kh1090
  • 43
  • 5

2 Answers2

0

Reapeat the same process for lblRightAddress also.

 // NSString *location = lblLeftAddress.text; I hust checked with your address it worked.
 NSString *location = @"296 Broadway Blvd Santa Monica CA 90016";
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:location
                     completionHandler:^(NSArray* placemarks, NSError* error){
                         if (placemarks && placemarks.count > 0) {
                             CLPlacemark *topResult = [placemarks objectAtIndex:0];
                             MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                             MKCoordinateRegion region = self.mapView.region;
                             region.span.longitudeDelta /= 8.0;
                             region.span.latitudeDelta /= 8.0;

                             [self.mapView setRegion:region animated:YES];
                             [self.mapView addAnnotation:placemark];
                         }
                     }
         ];

output

Nilesh Jha
  • 1,626
  • 19
  • 35
  • I placed this code in the viewDidLoad, but am still getting a blank map with no annotations. Should it be placed elsewhere? – kh1090 Aug 14 '16 at 09:02
  • give breakpoint and check whether you are getting some value in placemark or not. – Nilesh Jha Aug 14 '16 at 09:15
  • I checked with this ( NSString *location = @"296 Broadway Blvd Santa Monica CA 90016";) Its working .. I am attaching aScreen shots. – Nilesh Jha Aug 14 '16 at 09:17
  • Awesome! Good to know that it works. I'm going to try it right now. I appreciate the help Nilesh :) – kh1090 Aug 14 '16 at 21:21
  • Now, how can I draw a poly line between the two place mark annotations? THANK YOU! – kh1090 Aug 14 '16 at 22:48
  • https://iostpoint.wordpress.com/2016/08/14/draw-route-between-two-places-and-finding-distance-between-them-in-mkmapview/ – Nilesh Jha Aug 15 '16 at 05:32
0

Just change the coordinates here in source and destination.

#pragma mark - Show Route Direction

-(void)loadRoute
{
    MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(34.0207504,-118.69193) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

    MKMapItem *srcMapItem = [[MKMapItem alloc]initWithPlacemark:source];
    [srcMapItem setName:@""];

    MKPlacemark *destination = [[MKPlacemark alloc]initWithCoordinate:CLLocationCoordinate2DMake(37.757815,-122.5076406) addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil] ];

    MKMapItem *distMapItem = [[MKMapItem alloc]initWithPlacemark:destination];
    [distMapItem setName:@""];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    [request setSource:srcMapItem];
    [request setDestination:distMapItem];
    [request setTransportType:MKDirectionsTransportTypeAutomobile];

    MKDirections *direction = [[MKDirections alloc]initWithRequest:request];

    [direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {

        NSLog(@"response = %@",response);
        NSArray *arrRoutes = [response routes];
        [arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            MKRoute *rout = obj;

            MKPolyline *line = [rout polyline];
            [self.mapView addOverlay:line];
            NSLog(@"Rout Name : %@",rout.name);
            NSLog(@"Total Distance (in Meters) :%f",rout.distance);

            NSArray *steps = [rout steps];

            NSLog(@"Total Steps : %lu",(unsigned long)[steps count]);

            [steps enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSLog(@"Rout Instruction : %@",[obj instructions]);

            }];
        }];
    }];

}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{

    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor blueColor]];
        [renderer setLineWidth:3.0];
        return renderer;
    }
    return nil;
}

For more reference you can visit this here

Nilesh Jha
  • 1,626
  • 19
  • 35