如何使用 MapKit 放置图钉?

发布于 2024-08-15 06:37:18 字数 84 浏览 2 评论 0原文

我想允许我的应用程序的用户在地图中选择一个位置。本机地图具有“放置图钉”功能,您可以通过放置图钉来定位某些内容。我怎样才能在 MapKit 中做到这一点?

I would like to allow the user of my app to pick a location in the map. The native map has a "drop pin" feature where you can locate something by dropping a pin. How can I do this in MapKit?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事与诗 2024-08-22 06:37:18

您需要创建一个实现MKAnnotation协议的对象,然后将该对象添加到MKMapView

@interface AnnotationDelegate : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString * title;
    NSString * subtitle;
} 

实例化您的委托对象并将其添加到地图:

AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subt] autorelease];
[self._mapView addAnnotation:annotationDelegate];

地图将访问AnnotationDelegate 上的坐标属性,以找出将图钉放置在地图上的位置。

如果您想自定义注释视图,您需要在地图视图控制器上实现 MKMapViewDelegate viewForAnnotation 方法:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation

如果您想实现图钉拖动功能,您可以阅读关于处理注释触摸事件 Apple 操作系统参考库

您还可以查看这篇文章 使用 mapkit 进行拖放,它引用 GitHub 上的工作示例库。您可以通过检查 DDAnnotation 对象上的 _coordinates 成员来获取拖动注释的坐标。

You need to create an object that implements the MKAnnotation protocol and then add that object to the MKMapView:

@interface AnnotationDelegate : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString * title;
    NSString * subtitle;
} 

Instantiate your delegate object and add it to the map:

AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subt] autorelease];
[self._mapView addAnnotation:annotationDelegate];

The map will access the coordinate property on your AnnotationDelegate to find out where to put the pin on the map.

If you want to customize your annotation view you will need to implement the MKMapViewDelegate viewForAnnotation method on your Map View Controller:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation

If you would like to implement the pin drag functionality you can read about handling annotation touch events in the Apple OS Reference Library.

You can also check out this article on drag drop with mapkit which refers to a working sample library on GitHub. You can get the coordinates of the dragged annotation by checking the _coordinates member on the DDAnnotation object.

国产ˉ祖宗 2024-08-22 06:37:18

放置图钉的方法有多种,并且您没有在问题中指定使用哪种方法。第一种方法是以编程方式执行此操作,因为您可以使用 RedBlueThing 编写的内容,只是您并不真正需要自定义类(取决于您所针对的 iOS 版本)。对于 iOS 4.0 及更高版本,您可以使用此代码片段以编程方式放置图钉:

// Create your coordinate
CLLocationCoordinate2D myCoordinate = {2, 2};
//Create your annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = myCoordinate;
//If you want to clear other pins/annotations this is how to do it
for (id annotation in self.mapView.annotations) {
    [self.mapView removeAnnotation:annotation];
}
//Drop pin on map
[self.mapView addAnnotation:point];

如果您希望能够通过长按实际的地图视图来放置图钉,可以这样做:

// Create a gesture recognizer for long presses (for example in viewDidLoad)
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 0.5; //user needs to press for half a second.
[self.mapView addGestureRecognizer:lpgr]


- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = touchMapCoordinate;
    for (id annotation in self.mapView.annotations) {
        [self.mapView removeAnnotation:annotation];
    }
    [self.mapView addAnnotation:point];
}

如果您想枚举所有注释,只需使用两个片段中的代码即可。这是记录所有注释位置的方式:

for (id annotation in self.mapView.annotations) {
    NSLog(@"lon: %f, lat %f", ((MKPointAnnotation*)annotation).coordinate.longitude,((MKPointAnnotation*)annotation).coordinate.latitude);
}

There are multiple ways to drop a pin, and you don't specify which way to do it in your question. The first way is to do it programmatically, for that you can use what RedBlueThing wrote, except that you don't really need a custom class (depending on what version of iOS you are targetting). For iOS 4.0 and later you can use this snippet to programmatically drop a pin:

// Create your coordinate
CLLocationCoordinate2D myCoordinate = {2, 2};
//Create your annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = myCoordinate;
//If you want to clear other pins/annotations this is how to do it
for (id annotation in self.mapView.annotations) {
    [self.mapView removeAnnotation:annotation];
}
//Drop pin on map
[self.mapView addAnnotation:point];

If you want to be able to drop a pin by for example long pressing on the actual mapView, it can be done like this:

// Create a gesture recognizer for long presses (for example in viewDidLoad)
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 0.5; //user needs to press for half a second.
[self.mapView addGestureRecognizer:lpgr]


- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = touchMapCoordinate;
    for (id annotation in self.mapView.annotations) {
        [self.mapView removeAnnotation:annotation];
    }
    [self.mapView addAnnotation:point];
}

If you want to enumerate all the annotations, just use the code in both snippets. This is how you log positions for all annotations:

for (id annotation in self.mapView.annotations) {
    NSLog(@"lon: %f, lat %f", ((MKPointAnnotation*)annotation).coordinate.longitude,((MKPointAnnotation*)annotation).coordinate.latitude);
}
沒落の蓅哖 2024-08-22 06:37:18

您可以通过jcesarmobile在被点击时回答与 iphone mapkit 协调,您可以将图钉放置在任何位置,如下所示

// Define pin location
CLLocationCoordinate2D pinlocation;
pinlocation.latitude = 51.3883454 ;//set latitude of selected coordinate ;
pinlocation.longitude = 1.4368011 ;//set longitude of selected coordinate;

// Create Annotation point 
MKPointAnnotation *Pin = [[MKPointAnnotation alloc]init];
Pin.coordinate = pinlocation;
Pin.title = @"Annotation Title";
Pin.subtitle = @"Annotation Subtitle";

// add annotation to mapview
[mapView addAnnotation:Pin];

you can get touched location by ,jcesarmobile answer on get tapped coordinates with iphone mapkit and you can drop pin any where as bellow

// Define pin location
CLLocationCoordinate2D pinlocation;
pinlocation.latitude = 51.3883454 ;//set latitude of selected coordinate ;
pinlocation.longitude = 1.4368011 ;//set longitude of selected coordinate;

// Create Annotation point 
MKPointAnnotation *Pin = [[MKPointAnnotation alloc]init];
Pin.coordinate = pinlocation;
Pin.title = @"Annotation Title";
Pin.subtitle = @"Annotation Subtitle";

// add annotation to mapview
[mapView addAnnotation:Pin];
近箐 2024-08-22 06:37:18

您可能还需要设置 MapView Delegate。

[mkMapView setDelegate:self];

然后调用它的委托,viewForAnnotation

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView *pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:@"current"];
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.pinColor = MKPinAnnotationColorRed;
    return pinAnnotationView;
}

You might also need to set MapView Delegate.

[mkMapView setDelegate:self];

Then call its delegate, viewForAnnotation:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    MKPinAnnotationView *pinAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:@"current"];
    pinAnnotationView.animatesDrop = YES;
    pinAnnotationView.pinColor = MKPinAnnotationColorRed;
    return pinAnnotationView;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文