加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通过在iPhone中制作动态图钉??注释,从Google地图中选择位置

发布时间:2020-12-14 17:37:13 所属栏目:百科 来源:网络整理
导读:我想在iPhone上的Google地图上设计动态图钉注释,以便用户可以通过点击它来拖动图钉并将此图钉放在Google地图上以设置其选择的位置.用户放置引脚后,我想获得与该引脚对应的位置坐标和位置名称. 任何建议如何在iPhone中开发这个.谢谢 解决方法 在视图控制器中,
我想在iPhone上的Google地图上设计动态图钉注释,以便用户可以通过点击它来拖动图钉并将此图钉放在Google地图上以设置其选择的位置.用户放置引脚后,我想获得与该引脚对应的位置坐标和位置名称.
任何建议如何在iPhone中开发这个.谢谢

解决方法

在视图控制器中,您必须实现MKMapViewDelegate协议和LongPressGestureAware(我创建的)中的方法,以便在用户点击并按住屏幕时将引脚放在地图上.

您的注释应该实现MKAnnotation和MKReverseGeocoderDelegate协议.

我粘贴了一些可以帮助您的代码:

SimpleMapAnnotationViewController.h:

@interface SimpleMapAnnotationViewController : TTViewController<LongPressGestureAware,MKMapViewDelegate> {
    SimpleMapAnnotation *_dropPin;
    MKPinAnnotationView *_pinView;
}

SimpleMapAnnotationViewController.m:

#pragma mark -
#pragma mark LongPressGestureAware

-(void) initLongPressGestureRecognizer {
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [self.map addGestureRecognizer:longPressGesture];
    [longPressGesture release];
}

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {

    if([sender isMemberOfClass:[UILongPressGestureRecognizer class]] && (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateBegan)) {
    [self.map removeGestureRecognizer:sender]; //avoid multiple pins to appear when holding on the screen
    } 
    CGPoint point = [sender locationInView:self.map];
    CLLocationCoordinate2D theCoordinate = [self.map convertPoint:point toCoordinateFromView:self.map];
    self.dropPin = [[[SimpleMapAnnotation alloc] initWithCoordinate:theCoordinate] autorelease];
    [self.map addAnnotation:self.dropPin];
    [self performSelector:@selector(selectInitialAnnotation) withObject:nil afterDelay:0.5];
}

-(void)selectInitialAnnotation {
    [self.map selectAnnotation:[self.map.annotations objectAtIndex:0] animated:YES];
}

#pragma mark -
#pragma mark MKMapViewDelegate

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
    if (annotation == self.map.userLocation){
        return nil; //default to blue dot
    }
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"] autorelease];
    } else {
        pin.annotation = annotation;
    }

    pin.canShowCallout = YES;
    pin.draggable = YES;
    pin.animatesDrop = YES;
    pin.pinColor = MKPinAnnotationColorGreen;

    self.pinView = pin;
    self.dropPin.pinView = self.pinView;    
    return pin;
}


- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
//NSArray *annotations = self.map.annotations;
if (oldState == MKAnnotationViewDragStateDragging) {
    SimpleMapAnnotation *annotation = (SimpleMapAnnotation *)annotationView.annotation;
    [annotation updateSubtitle];    
}
if(newState == MKAnnotationViewDragStateEnding) {
    NSLog(@"drag finish");
}
}

SimpleMapAnnotation.h

@interface SimpleMapAnnotation : NSObject <MKAnnotation,MKReverseGeocoderDelegate> {
    CLLocationCoordinate2D _coordinate;
    NSString *_title;
    NSString *_subtitle;
    MKPinAnnotationView *_pinView; 
}

SimpleMapAnnotation.m

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {  
    self.coordinate = coordinate;
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:self.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
    self.subtitle = [NSString   stringWithFormat:@"%f %f",self.coordinate.latitude,self.coordinate.longitude]; 
return self;
}

#pragma mark -
#pragma mark MKReverseGeocoderDelegate

// this delegate is called when the reverseGeocoder finds a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    NSString *address = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStreetKey];
    self.title = address;
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"locationManager:%@ didFailWithError:%@",manager,error);
}

// this delegate is called when the reversegeocoder fails to find a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
     //invalid place
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读