2016-08-07 67 views
0

我試圖讓一個信息框與標記標準相同,以便在點擊我的多段線時顯示。當線條被點擊時,我得到了一個NSLog輸出,但現在我需要顯示信息框而不是NSLog。我見過一些Javascript的例子,但沒有客觀的c例子。點擊事件時自定義信息框

- (void)loadView { 

// Create a GMSCameraPosition 
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.551927 
                 longitude:-77.456292 
                  zoom:18]; 
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(37.551709, -77.456510); 
mapView.settings.myLocationButton = YES; 
mapView.myLocationEnabled = YES; 
self.view = mapView; 
mapView.delegate = self; 

GMSMutablePath *path = [GMSMutablePath path]; 
[path addCoordinate:CLLocationCoordinate2DMake(37.552243, -77.457415)]; 
[path addCoordinate:CLLocationCoordinate2DMake(37.551054, -77.455443)]; 

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path]; 

UILabel *myLabel = [[UILabel alloc] init]; 

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(labelTapped)]; 
tapGestureRecognizer.numberOfTapsRequired = 1; 
[myLabel addGestureRecognizer:tapGestureRecognizer]; 
myLabel.userInteractionEnabled = YES; 

polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor greenColor]]]; 
polyline.strokeWidth = 5.f; 
polyline.tappable = true; 
polyline.map = mapView; 

} 

- (void)mapView:(GMSMapView *)mapView didTapOverlay:(GMSOverlay *)overlay 
{ 
    NSLog(@"in didTapOverlay"); 
} 

@end 

回答

0

請查看iOS Maps教程中的Events指南。 下面是在Objective-C片段:

- (void)loadView { 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285 
                  longitude:103.848 
                   zoom:12]; 
    GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView.delegate = self; 
    self.view = mapView; 
} 

#pragma mark - GMSMapViewDelegate 

- (void)mapView:(GMSMapView *)mapView 
    didTapAtCoordinate:(CLLocationCoordinate2D)coordinate { 
    NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude); 
} 

下面是一個示範SO thread使用GMSMapViewDelegate

1.conform到GMSMapViewDelegate協議。

@interface YourViewController() <GMSMapViewDelegate> 
// your properties 
@end 
2.set your mapView_ delegate. 

mapView_.delegate = self; 
3.implement the GMSMapViewDelegate method 

- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker { 
    // your code 
} 

增加:marker.userData是有用的。你可以設置你需要的數據並使用它在- mapView:didTapInfoWindowOfMarker: