2015-10-17 118 views
0

這是我迄今所做的:如何在mapview上設置當前位置的監聽器?

_mapview.delegate = self; 
_mapview.showsUserLocation = YES; 
locationManager = [CLLocationManager new]; 
locationManager.delegate = self; 
locationManager.distanceFilter = kCLLocationAccuracyKilometer; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
//iOS 8 API change 
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){ 
    [locationManager requestWhenInUseAuthorization]; 
}else{ 
    [locationManager startUpdatingLocation]; 
} 
[locationManager startUpdatingLocation]; 
CLLocation *location = [locationManager location]; 
CLLocationCoordinate2D coordinate = [location coordinate]; 
self.mapview.region = MKCoordinateRegionMakeWithDistance(coordinate, 1000, 1000); 

我也得到這樣的觀點:

enter image description here

怎樣纔可以有一個監聽器,捕捉時,在當前定位銷用戶點擊。我試過,calloutAccessoryControlTapped不工作。

更新

我改成了這樣:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    NSLog(@"MKAnnotationView"); 

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"]; 
    annView.canShowCallout = YES; 
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    annView.rightCalloutAccessoryView=detailButton; 
} 

但後來我需要獲得當前位置的地址。但標題不是它的地址。我怎樣才能顯示當前位置地址的引腳或當用戶點擊引腳我得到的地址像門牌號碼。街道名稱。城市名。狀態:

static NSString *defaultPinID = @"pin"; 
MKPinAnnotationView *pinAnnotation = nil; 
pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:view.annotation reuseIdentifier:defaultPinID]; 

self.address = view.annotation.title; 
CLLocationCoordinate2D coor = [view.annotation coordinate]; 
_eventGeoLocation2 = [PFGeoPoint geoPointWithLatitude:coor.latitude longitude:coor.longitude]; 

回答

3

您可以使用MKPinAnnotationView提供的默認細節披露按鈕。這裏是代碼,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 

     MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"]; 
     annView.canShowCallout = YES; 
     UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
     annView.rightCalloutAccessoryView=detailButton; 
} 

一旦完成。你可以使用這樣的現有代理方法,

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    //Here, the annotation tapped can be accessed using view.annotation 

    NSLog(@"%@",view.annotation.title); 
    NSLog(@"%@",view.annotation.subtitle); 
    [self performSegueWithIdentifier:@"Your identifier" sender:view.annotation.title]; 
} 
+0

謝謝隊友。這工作正常,但我有一個問題。請看看更新 – Bernard

0

您是否嘗試調用GMSMapView的委託方法?

- (void)mapView:(GMSMapView *)mapView 
    didTapInfoWindowOfMarker:(GMSMarker *)marker; 
相關問題