-2

I want to load map on MKMapView.

Basically what i want to do is,

I want to load Particular Venue in my MapView.

My Database contains lot of Venues, according to requirement, i am fetching Venue and i want to load that Venue in my mapView.

for eg: I got this as Venue: @"100 Oxford Street, London, W1D 1LL, 020 7636 0933" from Database, then i want to load this location in my mapView

Thanks in advance.

EDIT:

    CLGeocoder *geocoder = [[CLGeocoder alloc] init];

    NSString *venue= @"100 Oxford Street, London, W1D 1LL, 020 7636 0933";
    [geocoder geocodeAddressString:venue completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if ([placemarks count] > 0)
         {
             CLPlacemark *placemark = [placemarks objectAtIndex:0];
             CLLocation *loc = placemark.location;
             CLLocationCoordinate2D _venue = loc.coordinate;
             NSLog(@"_venue:=%f",loc.coordinate);

             MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
             [venueAnnotation setCoordinate:_venue];
             [venueAnnotation setTitle:@"Venue"];
             [MapVw addAnnotation:venueAnnotation];
         }
     }
     ];
Krunal
  • 6,440
  • 21
  • 91
  • 155

3 Answers3

3
- (IBAction)BtnClick:(id)sender {

NSLog(@"Map ");


CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"100 Oxford Street, London, W1D 1LL, 020 7636 0933"
             completionHandler:^(NSArray* placemarks, NSError* error)
 {
     if (placemarks && placemarks.count > 0)
     {
         CLPlacemark *topResult = [placemarks objectAtIndex:0];
         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

         [self.MapView addAnnotation:placemark];

         CLLocationCoordinate2D _venue = placemark.coordinate;

         [self.MapView setCenterCoordinate:_venue];

         MKCoordinateRegion region = self.MapView.region;
         region.span.longitudeDelta = 1.0;
         region.span.latitudeDelta = 1.0;
         [self.MapView setRegion:region animated:YES];

     }

 }
 ];
}
Apple
  • 736
  • 1
  • 6
  • 24
  • [geocoder geocodeAddressString:@"100 Oxford Street, London, W1D 1LL, 020 7636 0933" completionHandler.... **But it gives Error** – Krunal Jul 29 '13 at 11:12
  • Yes, i wrote this: `CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:@"100 Oxford Street, London, W1D 1LL, 020 7636 0933" completionHandler:^(NSArray* placemarks, NSError* error){ if (placemarks && placemarks.count > 0) {CLPlacemark *topResult = [placemarks objectAtIndex:0]; MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult]; [self.MapVw addAnnotation:placemark]; } ];` – Krunal Jul 29 '13 at 11:16
  • See my **EDIT:** in the question i have tried that but doesn't load anything. – Krunal Jul 29 '13 at 11:34
  • did u bind MapVw and why dont you add placemark direct into annotation... ? – Apple Jul 29 '13 at 11:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34371/discussion-between-tapan-nathvani-and-krunal) – Apple Jul 29 '13 at 11:42
1

Steps:
1. Import CoreLocation.framework and MapKit.framework.
2. Use CoreLocation class Geocoder's method :

geoCodeAddressString:completionHandler:

NSString *venue= @"100 Oxford Street, London, W1D 1LL, 020 7636 0933";
 [_geoCoder geocodeAddressString:venue completionHandler:^(NSArray *placemarks, NSError *error)
     {
       if ([placemarks count] > 0)
       {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         CLLocation *loc = placemark.location;
         CLLocationCoordinate2D _venue = loc.coordinate;
      }
     });
 ]

[self performSelector:@selector(createVenueAnnotation) withObject:nil afterDelay:5];
 UIActivityIndicatorView *activity =  [[UIActivityIndicatorView alloc]init];
[_mapView addSubview:activity];
[activity startAnimating];

Step 3. If you want to place a MKPointAnnotation on the venue point.

- (void)createVenueAnnotation{
        [activity stopAnimating];
        [activity removeFromSuperview];
       MKPointAnnotation *venueAnnotation = [[MKPointAnnotation alloc]init];
        [venueAnnotation setCoordinate:_venue];
        [venueAnnotation setTitle:@"Venue"];
        [_mapView addAnnotation:venueAnnotation];
}

Step 4: Center your map around the venue.

// you need to call this function
- (void)centerMapAroundVenue
{
  MKMapRect rect = MKMapRectNull;
  MKMapPoint venuePoint = MKMapPointForCoordinate(_venue);
  rect = MKMapRectUnion(rect, MKMapRectMake(venuePoint .x, venuePoint .y, 0, 0));
  MKCoordinateRegion region = MKCoordinateRegionForMapRect(rect);
  [_mapView setRegion:region animated:YES];
}
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
  • See my **EDIT:** in the question i have tried that but doesn't load anything. – Krunal Jul 29 '13 at 11:34
  • Do you have internet connection? if you are using simulator, please open safari and check whether you have internet connection or not, same for device. – Puneet Sharma Jul 29 '13 at 11:37
  • No, i testing on Device and internet connection also working, my log shows `_venue:=51.516139` but map doesn't get loaded. – Krunal Jul 29 '13 at 11:39
  • You should use a.n indicator view to represent loading.. forward geocoding takes time .. I suggest you use like this: [self performSelector:@selector(drawVenueAnnotation) withObject:nil afterDelay:5]; Here delay of 5 seconds is given.. and drawVenueAnnotation is the method in Step 3 above. – Puneet Sharma Jul 29 '13 at 11:49
  • it just shows map of india, not the map of coordinate which i provided – Krunal Jul 29 '13 at 12:01
  • I am not getting your delay method, can you Edit your code ? what should i wrote in my method... – Krunal Jul 29 '13 at 12:21
0

Here is simple scenario :

  • Get the Co-ordinates of the Address using GeoCoder
  • Using the Co-ordinatation ,Add the Annotation to the MapView.

Do something Like this to get the CLlocationCoordinate :

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    double latitude = 0, longitude = 0;
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result) {
        NSScanner *scanner = [NSScanner scannerWithString:result];
        if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
            [scanner scanDouble:&latitude];
            if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
                [scanner scanDouble:&longitude];
            }
        }
    }
    CLLocationCoordinate2D center;
    center.latitude = latitude;
    center.longitude = longitude;
    return center;
}

Using the Center just add the annotation to the MapView.

[self.mapView addAnnotation:<annotationName>];  

Plz go through this link For more info.

Hope this helps.

Community
  • 1
  • 1
Kumar KL
  • 15,315
  • 9
  • 38
  • 60