MKMapView with address

后端 未结 5 1889
梦毁少年i
梦毁少年i 2021-02-05 15:25

is there a way to make the MKMapView place a pin with a given address? Without using the Coordinates

Thanks

5条回答
  •  礼貌的吻别
    2021-02-05 16:01

    Since this result is in the first page when you make a Google search, I think it's good to give a fresher solution than the Google geocoding (limited). It's better to use an Apple geocoder:

    NSString *location = @"your address";
    
    CLGeocoder *geocoder = [CLGeocoder new];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     if (error) {
                         NSLog(@"%@", error);
                     } else if ([placemarks count]) {
                         CLPlacemark *topResult = [placemarks firstObject];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
    
    
                         MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(placemark.coordinate, 5000, 5000);
    
                         [self.mapView setRegion:region animated:YES];
                         [self.mapView addAnnotation:placemark];
                     }
                 }];
    

提交回复
热议问题