2015-05-15 276 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); 

    } 
}