iphone - 通过在 iPhone 中进行动态图钉注释从 Google map 中选择位置

标签 iphone ios xcode google-maps

我想在 iPhone 的谷歌地图上设计一个动态图钉注释,这样用户可以通过点击图钉来拖动图钉,并将这个图钉放在谷歌地图上以设置其选择的位置。
用户放置pin 我想获取那个pin对应的位置坐标和位置名称。 关于如何在 iPhone 中开发这个的任何建议。
谢谢

最佳答案

在您的 View Controller 中,您必须实现来自 MKMapViewDelegate 协议(protocol)和 LongPressGestureAware(我创建的)的方法,以便在用户点击并按住屏幕时将图钉放在 map 上。

您的注释应实现 MKAnnotation 和 MKReverseGeocoderDelegate 协议(protocol)。

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

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
}

关于iphone - 通过在 iPhone 中进行动态图钉注释从 Google map 中选择位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8672192/

相关文章:

ios - "memory history"命令到底是什么?

ios - 从 View 中清除图层会导致应用程序在下一次迭代时崩溃

ios - 广场定位自动布局

ios - Vuforia + Unity 为 iPhone X iOS 11.4 黑屏构建

ios - 按位置从 iOS 照片库中获取照片

iphone - 如何保护来自 iPhone 的 JSON 请求?

ios - Coreplot 图缩放问题

iphone - 编码解码类错误的 int 属性

ios - xcode ios 模拟器不显示更新的选项卡栏 Controller

iPhone 一个 Controller ,多个 View