-2

我想加载地图MKMapView

基本上我想做的是,

我想Venue在我的 MapView 中加载 Particular。

我的数据库包含很多Venues,根据要求,我正在获取Venue并且我想将其加载到Venue我的 mapView 中。

例如:我从数据库中得到Venue: @"100 Oxford Street, London, W1D 1LL, 020 7636 0933",然后我想在我的 mapView 中加载这个位置

提前致谢。

编辑:

    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];
         }
     }
     ];
4

3 回答 3

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];

     }

 }
 ];
}
于 2013-07-29T11:09:31.090 回答
1

步骤:
1. 导入CoreLocation.frameworkMapKit.framework.
2.使用CoreLocationGeocoder的方法:

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. 如果你想在场地点上放置一个 MKPointAnnotation。

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

第 4 步:将您的地图以场地为中心。

// 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];
}
于 2013-07-29T11:16:36.110 回答
0

这是简单的场景:

  • 使用获取地址的坐标GeoCoder
  • 使用 Co-ordinatation ,将 Annotation 添加到 MapView。

做这样的事情来获得 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;
}

使用中心只需将注释添加到 MapView。

[self.mapView addAnnotation:<annotationName>];  

请通过此链接了解更多信息。

希望这可以帮助。

于 2013-07-29T11:19:00.663 回答