【问题标题】:MKMapview place pin at location (long/lat)MKMapview 将大头针放置在位置(长/纬度)
【发布时间】:2011-11-19 13:29:53
【问题描述】:

我有纬度和经度值,我需要能够在此位置放置图钉。

有人可以就如何解决这个问题提供一些建议吗?

【问题讨论】:

    标签: objective-c mkmapview mkannotation mkannotationview


    【解决方案1】:

    找到以下非常简单的解决方案,将引脚放置在CLLocationCoordinate2D定义的给定位置

    Drop Pin on MKMapView

    已编辑:

    CLLocationCoordinate2D  ctrpoint;
    ctrpoint.latitude = 53.58448;
    ctrpoint.longitude =-8.93772;
    AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:ctrpoint]; 
    [mapview addAnnotation:addAnnotation];
    [addAnnotation release];
    

    【讨论】:

    • 谢谢。使用的语法是什么:initWithCoordinate:ctrpoint]; - 即我如何添加我的经纬度?
    • 无需创建/使用自定义类来放置引脚。可以使用苹果提供的MKPointAnnotation类。
    【解决方案2】:

    你应该:
    1. 将 MapKit 框架添加到您的项目中。 2. 创建一个实现 MKAnnotation 协议的类。
    示例:

    Annotation.h

    @interface Annotation : NSObject <MKAnnotation> {
        NSString *_title;
        NSString *_subtitle;
    
        CLLocationCoordinate2D _coordinate;
    }
    
    // Getters and setters
    - (void)setTitle:(NSString *)title;
    - (void)setSubtitle:(NSString *)subtitle;
    
    @end
    

    注解.m

    @implementation Annotation
    
    #pragma mark -
    #pragma mark Memory management
    
    - (void)dealloc {
        [self setTitle:nil];
        [self setSubtitle:nil];
    
        [super dealloc];
    }
    
    #pragma mark -
    #pragma mark Getters and setters
    
    - (NSString *)title {
        return _title;
    }
    
    - (NSString *)subtitle {
        return _subtitle;
    }
    
    - (void)setTitle:(NSString *)title {    
        if (_title != title) {
            [_title release];
            _title = [title retain];
        }
    }
    
    - (void)setSubtitle:(NSString *)subtitle {
        if (_subtitle != subtitle) {
            [_subtitle release];
            _subtitle = [subtitle retain];
        }
    }
    
    - (CLLocationCoordinate2D)coordinate {
        return _coordinate;
    }
    
    - (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
        _coordinate = newCoordinate;
    }
    
    @end
    

    2。创建此类的实例并设置 lat/lon 属性
    3. 使用此方法将实例添加到 MKMapView 对象中:

    - (void)addAnnotation:(id<MKAnnotation>)annotation
    

    4。您应该设置地图的委托并实现以下方法:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        if ([annotation isKindOfClass:[MKUserLocation class]]) {
            return nil;
        }
    
        static NSString* ShopAnnotationIdentifier = @"shopAnnotationIdentifier";
        MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ShopAnnotationIdentifier];
        if (!pinView) {
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ShopAnnotationIdentifier] autorelease];
            pinView.pinColor = MKPinAnnotationColorRed;
            pinView.animatesDrop = YES;
        }
        return pinView;
    }
    

    【讨论】:

      【解决方案3】:

      这假设您已启用 ARC 并且您已包含 MapKit 框架。

      首先创建一个实现 MKAnnotation 协议的类。我们称之为 MapPinAnnotation。

      MapPinAnnotation.h

      @interface MapPinAnnotation : NSObject <MKAnnotation>
      
      @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
      @property (nonatomic, readonly) NSString* title;
      @property (nonatomic, readonly) NSString* subtitle;
      
      - (id)initWithCoordinates:(CLLocationCoordinate2D)location
                      placeName:(NSString *)placeName
                    description:(NSString *)description;
      
      @end
      

      MapPinAnnotation.m

      @implementation MapPinAnnotation
      
      @synthesize coordinate;
      @synthesize title;
      @synthesize subtitle;
      
      - (id)initWithCoordinates:(CLLocationCoordinate2D)location
                      placeName:(NSString *)placeName
                    description:(NSString *)description;
      {
        self = [super init];
        if (self)
        {    
          coordinate = location;
          title = placeName;
          subtitle = description;
        }
      
        return self;
      }
      
      @end
      

      然后使用以下命令将注释添加到地图:

      CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496,
                                                                    -119.70182);
      
      MapPinAnnotation* pinAnnotation = 
        [[MapPinAnnotation alloc] initWithCoordinates:coordinate
                                            placeName:nil
                                          description:nil];
      [mMapView addAnnotation:pinAnnotation];
      

      包含类必须实现 MKMapViewDelegate 协议。特别是,您必须定义以下函数来绘制图钉:

      - (MKAnnotationView *)mapView:(MKMapView *)mapView 
                  viewForAnnotation:(id <MKAnnotation>)annotation
      {
        if ([annotation isKindOfClass:[MKUserLocation class]])
        {
          return nil;
        }
      
        static NSString* myIdentifier = @"myIndentifier";
        MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];
      
        if (!pinView)
        {
          pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
          pinView.pinColor = MKPinAnnotationColorRed;
          pinView.animatesDrop = NO;
        }
        return pinView;
      }
      

      在这个例子中,MKAnnotation 的 title 和 subtitle 成员变量没有被使用,但是它们可以显示在委托函数中。

      【讨论】:

      • 我不断收到 -[MapPinAnnotation setCoordinate:]: unrecognized selector sent to instance 错误。
      【解决方案4】:
      -(MKPointAnnotation *)showClusterPoint:(CLLocationCoordinate2D)coords withPos:(NSString *)place
      {
          float  zoomLevel = 0.5;
          region = MKCoordinateRegionMake (coords, MKCoordinateSpanMake (zoomLevel, zoomLevel));
          [mapView setRegion: [mapView regionThatFits: region] animated: YES];
      
          point = [[MKPointAnnotation alloc]init];
          point.coordinate = coords;
          point.title=place;
      
          [mapView addAnnotation:point];
      
          return point;
      }
      

      【讨论】:

        【解决方案5】:

        斯威夫特版本

                let location = CLLocationCoordinate2DMake(13.724362, 100.515342);
                let region = MKCoordinateRegionMakeWithDistance(location, 500.0, 700.0)
                self.mkMapView.setRegion(region, animated: true)
                // Drop a pin
                let dropPin = MKPointAnnotation();
                dropPin.coordinate = location;
                dropPin.title = "Le Normandie Restaurant";
                self.mkMapView.addAnnotation(dropPin);
        

        【讨论】:

          【解决方案6】:
          Please use this code. its working fine.
          -(void)addAllPinsOnMapView
          {
          MKCoordinateRegion region = mapViewOffer.region;
          region.center = CLLocationCoordinate2DMake(23.0225, 72.5714);
          region.span.longitudeDelta= 0.1f;
          region.span.latitudeDelta= 0.1f;
          [mapViewOffer setRegion:region animated:YES];
          mapViewOffer.delegate=self;
          
          MKPointAnnotation *mapPin = [[MKPointAnnotation alloc] init];
          CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(23.0225, 72.5714);
          mapPin.title = @"Title";
          mapPin.coordinate = coordinate;
          [mapViewOffer addAnnotation:mapPin];
          }
          - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:
          (MKAnnotationView *)view
          {
          NSLog(@"%@",view.annotation.title);
          NSLog(@"%f",view.annotation.coordinate.latitude);
          NSLog(@"%f",view.annotation.coordinate.longitude);
          }
          
          - (MKAnnotationView *)mapView:(MKMapView *)theMapView 
          viewForAnnotation:(id <MKAnnotation>)annotation
          {
          if ([annotation isKindOfClass:[MKUserLocation class]])
          {
              ((MKUserLocation *)annotation).title = @"Current Location";
              return nil;
          }
          else
          {
              MKAnnotationView *pinView = nil;
              static NSString *defaultPinID = @"annotationViewID";
              pinView = (MKAnnotationView *)[mapViewOffer dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
              if ( pinView == nil ){
                  pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
              }
              pinView.canShowCallout = YES;
              pinView.image = [UIImage imageNamed:@"placeholder"];
          
              UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
              //    [infoButton addTarget:self action:@selector(infoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
              pinView.rightCalloutAccessoryView = infoButton;
              pinView.rightCalloutAccessoryView.tag=1;
              return pinView;
          }
          }
          - (MKPinAnnotationView*)myMap:(MKMapView*)myMap viewForAnnotation:
          (id<MKAnnotation>)annotation{
          
          MKPinAnnotationView *pin = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomPin"];
          
          UIImage *icon = [UIImage imageNamed:@"bustour.png"];
          UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(8,0,32,37)];
          
          if(icon == nil)
              NSLog(@"image: ");
          else
              NSLog(@"image: %@", (NSString*)icon.description);
          
          [iconView setImage:icon];
          [pin addSubview:iconView];
          pin.canShowCallout = YES;
          pin.pinColor = MKPinAnnotationColorPurple;
          
          return pin;
          }
          
          - (IBAction)btnLocateMe:(UIButton *)sender
          {
          [mapViewOffer setCenterCoordinate:mapViewOffer.userLocation.location.coordinate animated:YES];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-08-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多