在MKMapView上加载地图视图

时间:2013-07-29 11:06:09

标签: iphone ios ipad google-maps mkmapview

我想在MKMapView上加载地图。

基本上我想做的是,

我想在MapView中加载特殊Venue

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

例如:我从数据库得到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];
         }
     }
     ];

3 个答案:

答案 0 :(得分: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];

     }

 }
 ];
}

答案 1 :(得分: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];

步骤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];
}

答案 2 :(得分:0)

这是一个简单的场景:

  • 使用GeoCoder
  • 获取地址的坐标
  • 使用协调,将注释添加到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>];  

Plz go through this link了解更多信息。

希望这有帮助。