2015-05-15 182 views
0

我试图找到一个点,当我触摸谷歌地图的坐标,但到目前为止,我所做的一切都不工作。理想情况下,我希望能够在点击地图时获取地图上某个点的坐标。之后,我将能够在该位置放置一针。谷歌地图:获取坐标

我尝试过其他的尝试,如:

- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer   
{ 
if (gestureRecognizer.state != UIGestureRecognizerStateBegan) 
    return; 

CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView]; 
CLLocationCoordinate2D location = 
    [self.mapView convertPoint:touchPointtoCoordinateFromView:self.mapView]; 

NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude); 

} 

,但这不会工作,我得到了一个错误说 “为GMSMapView中没有明显的@interface声明选择convertpoint tocoordinatefromview

我也有点。失去了,不知道该怎么做。

最佳, Ĵ

回答

0

谷歌地图点击某个点时会给你一个委托。您获得这些活动的方式是,

您将您的课程符合GMSMapViewDelegate并实施方法:- mapView:didTapAtCoordinate:。确保您将mapview的代表设置为self

你可以找到关于GMSMapViewDelegatehere的全部信息。

0
CLLocationCoordinate2D touchMapCoordinate; 

- (void)viewDidLoad { 

    [self setUpGesture:YES]; 
} 

- (void)setUpGesture:(BOOL)state 
{ 
    if (state == YES) 
    { 
     self.longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                     action:@selector(handleLongPress:)]; 
     self.longPress.delegate = self; 
     [self.view addGestureRecognizer:self.longPress]; 

    } 
    else 
    { 
     [self.mapView removeGestureRecognizer:self.longPress]; 
     self.longPress = nil; 
    } 

} 

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer 
{ 
    if (recognizer.state == UIGestureRecognizerStateBegan) 
    { 
     CGPoint longPressPoint = [recognizer locationInView:self.view]; 
     [self dropPinAtPoint:longPressPoint]; 
    } 
} 


- (void)dropPinAtPoint:(CGPoint)pointToConvert 
{ 
touchMapCoordinate = [self.mapView convertPoint:pointToConvert 
           toCoordinateFromView:self.view]; 
} 

{ 


       NSMutableDictionary *dict=[[NSMutableDictionary alloc] init]; 
       [dict setObject:[NSString stringWithFormat:@"%f",touchMapCoordinate.latitude] forKey:@"latitude"]; 
       [dict setObject:[NSString stringWithFormat:@"%f",touchMapCoordinate.longitude] forKey:@"longitude"]; 
       [dict setObject:Str_pin forKey:@"title"]; 

       [ApplicationDelegate.Array_LongpressItem addObject:dict]; 


       [[NSUserDefaults standardUserDefaults] setObject:ApplicationDelegate.Array_LongpressItem forKey:@"ApplicationDelegate.Array_LongpressItem"]; 


        for (id annotation in self.mapView.annotations) 
             { 
              [self.mapView removeAnnotation:annotation]; 

             } 


       [self loadRoute]; 

       // add the overlay to the map 
       if (nil != self.routeLine) { 
        [self.mapView addOverlay:self.routeLine]; 
       } 

       // zoom in on the route. 
       [self zoomInOnRoute]; 


       for (int i=0; i<[ApplicationDelegate.Array_LongpressItem count]; i++) 
       { 
//     NSString *pinTitle = Str_pin; 
//     // NSString *subCoordinates = [NSString stringWithFormat: @"%f, %f", convertedPoint.latitude, convertedPoint.longitude]; 
//      
//     MyAnnotation *point = [[MyAnnotation alloc] init]; 
//     point.coordinate = touchMapCoordinate; 
//     point.tag=[NSNumber numberWithInt:2000]; 
//     point.title = pinTitle; 
//      
//     // for (id annotation in self.mapView.annotations) 
//     // { 
//     //  [self.mapView removeAnnotation:annotation]; 
//     // 
//     // } 
//     [self.mapView addAnnotation:point]; 

        CLLocationDegrees latitude = [[[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"latitude"] floatValue]; 
        CLLocationDegrees longitude = [[[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"longitude"]floatValue]; 
        // create our coordinate and add it to the correct spot in the array 
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude); 
        ann[i] = [[MyAnnotation alloc] init]; 
        ann[i].coordinate =coordinate; 
        ann[i].tag=[NSNumber numberWithInt:2000]; 
        ann[i].title = [[ApplicationDelegate.Array_LongpressItem objectAtIndex:i] objectForKey:@"title"]; 
        [_mapView addAnnotation:ann[i]]; 

       } 
      } 

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation*) annotation 
{ 
if ([annotation.tag intValue]==2000) 
    { 
     annView.annotation=annotation; 
     annView.canShowCallout = YES; 
     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     annView.animatesDrop=YES; 
     button1.tag=[annotation.tag intValue]; 
     [button1 addTarget:self action:@selector(btn_clicked:) forControlEvents:UIControlEventTouchUpInside]; 
     annView.rightCalloutAccessoryView = button1; 
     annView.calloutOffset = CGPointMake(0, 32); 

    } 
}