问题描述
我有两个问题,
-
如何根据用户的触地事件在MKMapkitView上创建叠加层?即为了简单起见,用户触摸它并创建一个MKCircle叠加
How to create an overlay on a MKMapkitView from user's touch down events? i.e. To keep it simple, the user touches down and it creates a MKCircle overlay
地图应用程序如何在触摸时实现掉针?有人知道或有一些关于如何完成类似事情的代码示例吗?
How does the Maps application implements the "dropped pin" on touch down? Anybody knows or have some code examples on how to accomplish something similar?
任何指针都将不胜感激。我一直在谷歌搜索和阅读大量的文档,但没有取得太大的成功,你可以看到。
Any pointers would be greatly appreciated. I've been googling and reading lots of docs without much success as you can see.
推荐答案
下面是一个创建一个例子的例子圈出并放下用户触摸并固定手指1秒钟的针脚。它使用UILongPressGestureRecognizer,无论地图初始化的地方都添加到mapView(例如viewDidLoad)。
Below is an example that creates a circle and drops a pin where the user touches and holds their finger for 1 second. It uses a UILongPressGestureRecognizer which is added to the mapView wherever the map is initialized (eg. viewDidLoad).
确保mapView的委托也已设置。
Make sure the mapView's delegate is set also.
// In viewDidLoad or where map is initialized...
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //user must hold for 1 second
[mapView addGestureRecognizer:lpgr];
[lpgr release];
...
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
//add pin where user touched down...
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
//add circle with 5km radius where user touched down...
MKCircle *circle = [MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:5000];
[mapView addOverlay:circle];
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
circleView.fillColor = [UIColor redColor];
return circleView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = @"Annotation";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = YES;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
这篇关于从MKMapView上的用户交互创建叠加层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!