【问题标题】:Load Map View on MKMapView在 MKMapView 上加载地图视图
【发布时间】:2013-07-29 11:06:08
【问题描述】:

我想在MKMapView 上加载地图。

基本上我想做的是,

我想在我的 MapView 中加载特定的 Venue

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

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

提前致谢。

编辑:

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

【问题讨论】:

  • 使用地理编码器从地址获取经纬度并向您的 MKMapView 添加注释
  • @property (weak, nonatomic) IBOutlet MKMapView *MapVw; 我也连接了代理。

标签: iphone ios ipad google-maps mkmapview


【解决方案1】:
- (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];

     }

 }
 ];
}

【讨论】:

  • [geocoder geocodeAddressString:@"100 Oxford Street, London, W1D 1LL, 020 7636 0933" completionHandler.... 但它给​​出了错误
  • 是的,我写了这个: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]; } ];
  • 查看我的 EDIT: 在我尝试过但没有加载任何内容的问题中。
  • 你绑定了 MapVw 为什么不直接将地标添加到注释中...?
【解决方案2】:

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

【讨论】:

  • 查看我的 EDIT: 在我尝试过但没有加载任何内容的问题中。
  • 你有互联网连接吗?如果您使用的是模拟器,请打开 safari 并检查您是否有互联网连接,对于设备也是如此。
  • 不,我在设备和互联网连接上进行测试也正常,我的日志显示 _venue:=51.516139 但地图没有加载。
  • 你应该使用a.n指示器视图来表示加载..前向地理编码需要时间..我建议你这样使用:[self performSelector:@selector(drawVenueAnnotation) withObject:nil afterDelay:5];这里给出了5秒的延迟..而drawVenueAnnotation就是上面第3步中的方法。
  • 它只显示印度地图,而不是我提供的坐标地图
【解决方案3】:

这是一个简单的场景:

  • 使用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;
}

使用 Center 只需将注解添加到 MapView。

[self.mapView addAnnotation:<annotationName>];  

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

希望这会有所帮助。

【讨论】:

  • 查看我的 EDIT: 在我尝试过但没有加载任何内容的问题中。
  • 你得到的NSLog(@"_venue:=%f",loc.coordinate);
  • 您显示的代码对我来说工作正常。检查与mapView相关的..
猜你喜欢
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多