ero*_*ppa 18 iphone google-maps user-input mkmapview mkannotation
我想允许我的应用的用户在地图中选择一个位置.原生地图有一个"drop pin"功能,您可以通过放置引脚来定位.我怎么能在MapKit中做到这一点?
Red*_*ing 27
您需要创建一个实现MKAnnotation协议的对象,然后将该对象添加到MKMapView:
@interface AnnotationDelegate : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
}
Run Code Online (Sandbox Code Playgroud)
实例化您的委托对象并将其添加到地图:
AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subt] autorelease];
[self._mapView addAnnotation:annotationDelegate];
Run Code Online (Sandbox Code Playgroud)
地图将访问AnnotationDelegate上的坐标属性,以找出将图钉放在地图上的位置.
如果要自定义注释视图,则需要在Map View Controller上实现MKMapViewDelegate viewForAnnotation方法:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
Run Code Online (Sandbox Code Playgroud)
如果您想实现引脚拖动功能,可以阅读有关在Apple OS参考库中处理注释触摸事件的信息.
您还可以使用mapkit 查看有关拖放的文章,该文章引用了GitHub上的工作示例库.您可以通过检查获得的拖累注释的坐标_coordinates的会员DDAnnotation对象.
Jug*_*teR 23
有多种方法可以放置引脚,并且您没有指定在问题中使用哪种方法.第一种方法是以编程方式执行,因为您可以使用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];
Run Code Online (Sandbox Code Playgroud)
如果您希望能够通过长按实际的mapView来放置引脚,可以这样做:
// 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];
}
Run Code Online (Sandbox Code Playgroud)
如果要枚举所有注释,只需使用两个片段中的代码即可.这是您记录所有注释的位置的方法:
for (id annotation in self.mapView.annotations) {
NSLog(@"lon: %f, lat %f", ((MKPointAnnotation*)annotation).coordinate.longitude,((MKPointAnnotation*)annotation).coordinate.latitude);
}
Run Code Online (Sandbox Code Playgroud)
你可以通过japsarmobile回答来获取触摸位置,使用iphone mapkit获取tapped 坐标,你可以放下任何地方的引脚
// 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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31088 次 |
| 最近记录: |