Map 注释未拖动
MapPin.h:Map 注释未拖动,map,drag-and-drop,annotations,mapkit,viewcontroller,Map,Drag And Drop,Annotations,Mapkit,Viewcontroller,MapPin.h: #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface MapPin : NSObject<MKAnnotation> { CLLocationCoordinate2D coordinate; NSString *title; NSString *subtitle; } @property (nonatomic,readwrite,assign)CLLo
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic,readwrite,assign)CLLocationCoordinate2D coordinate;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end
在ViewController.h中,我采用了MKMapViewDelegate协议。
这是ViewController.m:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[self.MyMap dequeueReusableAnnotationViewWithIdentifier:@"myPin"];
if(pin == nil){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myPin"];
}
else{
pin.annotation=annotation;
}
pin.draggable=YES;
return pin;
}
//Add Location button
- (IBAction)AddPin:(id)sender {
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
MKCoordinateRegion region;
region.center=center;
region.span.latitudeDelta=0.2f;
region.span.longitudeDelta=0.2f;
MapPin *myPin = [[MapPin alloc]initWithCoordinates:self.MyMap.centerCoordinate placeName:@"LoMinder!" description:@"Set Location for LoMinder!"];
[_MyMap addAnnotation:myPin];
}
//Dropping the pin:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
if (newState == MKAnnotationViewDragStateStarting)
{
[_MyMap setRegion:myRegion animated:YES];
}
if(newState == MKAnnotationViewDragStateDragging)
{
myRegion.center=annotationView.annotation.coordinate;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
if(newState==MKAnnotationViewDragStateEnding)
{
arrived=self.MyMap.centerCoordinate;
center=annotationView.annotation.coordinate;
myRegion.center=center;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
MKPinAnnotationView*pin=(MKPinAnnotationView*)[self.MyMap dequeueReusableAnnotationViewWithIdentifier:@“myPin”];
如果(引脚==零){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“myPin”];
}
否则{
pin.annotation=注释;
}
pin.draggable=是;
回位销;
}
//添加位置按钮
-(iAction)AddPin:(id)发送方{
位置协调中心;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
协调区域;
区域。中心=中心;
region.span.latitudeDelta=0.2f;
区域跨度纵向德尔塔=0.2f;
MapPin*myPin=[[MapPin alloc]initWithCoordinates:self.MyMap.centerCoordinate地名:@“LoMinder!”说明:@“设置LoMinder的位置!”;
[_mymapaddannotation:myPin];
}
//放下别针:
-(void)mapView:(MKMapView*)mapView
注释视图:(MKAnnotationView*)注释视图
didChangeDragState:(MKAnnotationViewDragState)新闻状态
fromOldState:(MKAnnotationViewDragState)oldState
{
//区域
MK协调区域myRegion;
//居中
位置协调中心;
如果(newState==MKAnnotationViewDragStateStarting)
{
[_mymapsetRegion:myRegion动画:是];
}
如果(newState==mkAnnotationViewDragStateDraging)
{
myRegion.center=annotationView.annotation.coordinate;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_mymapsetRegion:myRegion动画:是];
}
如果(newState==MKAnnotationViewDragStateEnding)
{
到达=self.MyMap.centerCoordinate;
中心=annotationView.annotation.coordinate;
myRegion.center=center;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_mymapsetRegion:myRegion动画:是];
}
}
/////
我还将MapPin.h导入了ViewController。
当我运行我的应用程序时,我应该按ord er中的Add Location(添加位置)按钮来获取注释pin,我按下按钮,注释pin出现在地图上,但问题是当我尝试拖动它时,它似乎无法拖动。
请大家帮忙。
谢谢:)通过更改MapPin.m中的title方法解决
使其返回除nil以外的任何值。通过更改MapPin.m中的title方法解决 让它返回除零以外的任何内容
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[self.MyMap dequeueReusableAnnotationViewWithIdentifier:@"myPin"];
if(pin == nil){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myPin"];
}
else{
pin.annotation=annotation;
}
pin.draggable=YES;
return pin;
}
//Add Location button
- (IBAction)AddPin:(id)sender {
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
MKCoordinateRegion region;
region.center=center;
region.span.latitudeDelta=0.2f;
region.span.longitudeDelta=0.2f;
MapPin *myPin = [[MapPin alloc]initWithCoordinates:self.MyMap.centerCoordinate placeName:@"LoMinder!" description:@"Set Location for LoMinder!"];
[_MyMap addAnnotation:myPin];
}
//Dropping the pin:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
if (newState == MKAnnotationViewDragStateStarting)
{
[_MyMap setRegion:myRegion animated:YES];
}
if(newState == MKAnnotationViewDragStateDragging)
{
myRegion.center=annotationView.annotation.coordinate;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
if(newState==MKAnnotationViewDragStateEnding)
{
arrived=self.MyMap.centerCoordinate;
center=annotationView.annotation.coordinate;
myRegion.center=center;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
}