5

Tôi đang sử dụng Google Maps trong Ứng dụng iOS và đã triển khai cửa sổ thông tin tùy chỉnh để hiển thị tiêu đề của điểm đánh dấu.
Bây giờ, tôi đã thêm một nút trên cửa sổ thông tin tùy chỉnh đó nhưng vấn đề của tôi là phương thức hành động nút không được gọi.Nút trên Thông tin tùy chỉnh Cửa sổ không nhận được hành động trong ios

CustomInfoWindow.h

#import <UIKit/UIKit.h> 

@interface CustomInfoWindow : UIView 
@property (nonatomic,weak) IBOutlet UILabel *addressLabel; 
@property(nonatomic) IBOutlet UIButton *button; 
@end 

và trong infoWindow.xib, tôi đã thêm

  • UILabel gọi addressLabel
  • UIButton gọi button

ViewController.h

#import "CustomInfoWindow.h" 

@interface viewController : UIViewController<GMSMapViewDelegate> 
{ 
    GMSMapView *mapView; 
} 
@end 

ViewController.m

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker 
{ 
    NSLog(@"Mrker Tapped"); 

    CustomInfoWindow *infoWindow = [[[NSBundle mainBundle]loadNibNamed:@"infoWindow" 
                   owner:self 
                   options:nil] objectAtIndex:0]; 
    infoWindow.addressLabel.text = marker.title;  

    [infoWindow.button addTarget:self action:@selector(ButtonPressed) 
          forControlEvents:UIControlEventTouchUpInside]; 
    return infoWindow; 
} 

-(void)ButtonPressed 
{ 
    NSLog(@"Button Pressed"); 
} 

Về cơ bản ... ButtonPressed phương pháp không cháy.

+0

không 'infoWindow.addressLabel.text = marker.title; 'làm việc? (_just ask_) – staticVoidMan

+0

Ya làm việc nó đang hiển thị địa chỉ trên nhãn đó. – iSuresh

Trả lời

5

Tôi chưa sử dụng Google Maps SDK nhưng sau khi làm theo một vài liên kết, có vẻ như những gì bạn đang cố gắng thực hiện có thể không dễ dàng.

ref: https://developers.google.com/maps/documentation/android/infowindows
PS: Tài liệu này có thể là Android nhưng dường như nó cũng đúng cho iOS.

Để quote:

Lưu ý: Cửa sổ thông tin được rút ra không phải là một live view. Chế độ xem là được hiển thị dưới dạng hình ảnh (sử dụng View.draw(Canvas)) tại thời điểm được trả lại. Điều này có nghĩa là mọi thay đổi tiếp theo cho chế độ xem sẽ không được phản ánh bởi cửa sổ thông tin trên bản đồ. Để cập nhật cửa sổ thông tin sau (ví dụ: sau khi hình ảnh đã được tải), hãy gọi showInfoWindow(). Hơn nữa, cửa sổ thông tin sẽ không tôn trọng bất kỳ tương tác nào điển hình cho chế độ xem bình thường như các sự kiện chạm hoặc cử chỉ. Tuy nhiên, bạn có thể nghe sự kiện nhấp chuột chung trên toàn bộ cửa sổ thông tin là được mô tả trong phần bên dưới.

Bạn có thể thực hiện phương pháp đại biểu GMSMapViewDelegate's -didTapInfoWindowOfMarker: để kiểm tra xem infowindow đã được khai thác chưa.
(nhược điểm là toàn bộ infowindow trở thành một nút)


liên kết khác:

similar question 1
similar question 2

0

Nhìn vào mã này của bạn -

[infoWindow.button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

trong này, tôi đoán tên nút của bạn nên bắt đầu với vốn "B" như: -

[infoWindow.Button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

kể từ khi bạn có @property (nonatomic) Nút IBOutlet UIButton *;

+0

Không, nó không hoạt động. – iSuresh

+0

sau đó sử dụng điều này: [infoWindow.button addTarget: infoWindow action: @selector (ButtonPressed) forControlEvents: UIControlEventTouchUpInside]; và thực hiện buttonPressed trong lớp CustomInfoWindow. – borncrazy

+0

Tôi nghĩ rằng nếu nó là một vấn đề của nút hoặc nút thì nó sẽ cung cấp cho các lỗi tại thời gian biên dịch ... – svrushal

0
  • Đầu tiên viết CustomInfoWindow *infoWindow thuộc tính trong tệp tiêu đề .h.

  • Sau đó tổng hợp trong tập tin lớp .m và sau đó cố gắng thêm mục tiêu

0

Tôi nghĩ rằng infoWindow là quá nhỏ và các nút trên đó đặt bên ngoài nó.

Thử kiểm tra khung thông tinWindow. Ngoài ra, bạn có thể áp dụng bất kỳ màu nền nào cho infoWindow. Hoặc làm infoWindow.clipToBounds=YES. Nếu nút được đặt ra bên xem thì nó chắc chắn bị cắt bớt.

Thay đổi này

[infoWindow.button addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

để

[infoWindow.button addTarget:infoWindow action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

Và tuyên bố ButtonPressed trong CustomInfoWindow lớp.

0

Bạn không thể thêm một nút bên trong một infoWindow.

Để làm điều đó, bạn phải tạo cửa sổ thông tin tùy chỉnh của riêng mình và thêm nút vào đó.

map_ViewController.h

#import <UIKit/UIKit.h> 
#import <GoogleMaps/GoogleMaps.h> 

#import "CustomInfoWindow.h" 

@interface Map_ViewController : UIViewController <GMSMapViewDelegate>{ 
    BOOL canHideInfoWindow; 
} 
@property (strong, nonatomic) IBOutlet GMSMapView *map; 
@property (strong, nonatomic) CustomInfoWindow *displayedInfoWindow; 
@property BOOL markerTapped; 
@property BOOL cameraMoving; 
@property BOOL idleAfterMovement; 
@property (strong, nonatomic) GMSMarker *currentlyTappedMarker; 
@property (assign, nonatomic) CLLocationCoordinate2D position; 


@end 

Map_ViewController.m

#import "Map_ViewController.h" 

@interface Map_ViewController() 
@end 
@implementation Map_ViewController 



#pragma mark - viewDidLoad 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //get user position, i wont put all the code, only the code necessary for this topic 
    self.position =CLLocationCoordinate2DMake(POSITION); 

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.position.latitude longitude:self.position.longitude zoom:15 bearing:0 viewingAngle:60]; 

    _map = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    self.view = _map; 
    [self createMarkers] 
} 






#pragma mark create markers 
-(void)createMarkers{ 
    //settings(controls) on map 
    _map.delegate = self; 
    self.edgesForExtendedLayout = UIRectEdgeNone; 
    _map.accessibilityElementsHidden = NO; 
    _map.settings.compassButton  = YES; 
    _map.settings.myLocationButton = YES; 
    _map.myLocationEnabled   = YES; 
    [_map setMinZoom:8 maxZoom:19 ]; 

    //remove all markers before to create the new one 
    [_map clear]; 

    //this code above will center all markers on the map 
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; 
    //creates pin markers 

    //for this code, i used an model to store all markers.. again, i wont write it here.. so create by yourself the loop for it 
    for (marker in markersArray){ 
     GMSMarker *marker = [[GMSMarker alloc] init]; 

     marker.position = CLLocationCoordinate2DMake(LAtLongPosition); 

     marker.appearAnimation = kGMSMarkerAnimationPop; 
     marker.userData = marker; 
     marker.map = _map; 
     bounds = [bounds includingCoordinate:marker.position]; 
    } 

    //include user location too 
    bounds = [bounds includingCoordinate:self.position]; 


    //center the map 
    [_map animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50]]; 
} 











#pragma mark - GoogleMaps Delegate 
// Since we want to display our custom info window when a marker is tapped, use this delegate method 
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{ 

    // A marker has been tapped, so set that state flag 
    self.markerTapped = YES; 

    // If a marker has previously been tapped and stored in currentlyTappedMarker, then nil it out 
    if(self.currentlyTappedMarker) { 
     self.currentlyTappedMarker = nil; 
    } 

    // make this marker our currently tapped marker 
    self.currentlyTappedMarker = marker; 

    // if our custom info window is already being displayed, remove it and nil the object out 
    if([self.displayedInfoWindow isDescendantOfView:self.view]) { 
     [self.displayedInfoWindow removeFromSuperview]; 
     self.displayedInfoWindow = nil; 
    } 

    /* animate the camera to center on the currently tapped marker, which causes 
    mapView:didChangeCameraPosition: to be called */ 
    GMSCameraUpdate *cameraUpdate = [GMSCameraUpdate setTarget:marker.position]; 
    [_map animateWithCameraUpdate:cameraUpdate]; 

    return YES; 
} 





- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{ 
    cameraMoving state flag to YES 
    if(self.markerTapped) { 
     self.cameraMoving = YES; 
    } 

    //Move the custom info window with the map 
    CGPoint markerPoint = [_map.projection pointForCoordinate:self.currentlyTappedMarker.position]; 
    CGRect frame = self.displayedInfoWindow.bounds; 
    frame.origin.y = markerPoint.y - self.displayedInfoWindow.frame.size.height - 15 ; 
    frame.origin.x = markerPoint.x - self.displayedInfoWindow.frame.size.width/2; 
    self.displayedInfoWindow.frame = frame; 
} 






/* If the map is tapped on any non-marker coordinate, reset the currentlyTappedMarker and remove our 
custom info window from self.view */ 
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{ 
    if(self.currentlyTappedMarker) { 
     self.currentlyTappedMarker = nil; 
    } 

    if([self.displayedInfoWindow isDescendantOfView:self.view]) { 
     [self.displayedInfoWindow removeFromSuperview]; 
     self.displayedInfoWindow = nil; 
    } 
} 







#pragma mark create infoWindow 
// This method gets called whenever the map was moving but has now stopped 
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position{ 
    /* if we got here and a marker was tapped and our animate method was called, then it means we're ready 
    to show our custom info window */ 
    if(self.markerTapped && self.cameraMoving) { 

     infosMarker = self.currentlyTappedMarker.userData; 

     // reset our state first 
     self.cameraMoving = NO; 
     self.markerTapped = NO; 
     self.idleAfterMovement = YES; 


     //CREATE YOUR INFO WINDOW VIEW (CustomInfoWindow : UIView)and load it 
     self.displayedInfoWindow = [[[NSBundle mainBundle] loadNibNamed:@"CustomInfoWindow" owner:self options:nil] objectAtIndex:0]; 
     CGPoint markerPoint = [_map.projection pointForCoordinate:self.currentlyTappedMarker.position]; 
     CGRect frame = self.displayedInfoWindow.bounds; 
     frame.origin.y = markerPoint.y - self.displayedInfoWindow.frame.size.height - 15; 
     frame.origin.x = markerPoint.x - self.displayedInfoWindow.frame.size.width/2; 
     self.displayedInfoWindow.frame = frame; 





     [self.displayedInfoWindow.YOURBUTTON addTarget:self action:@selector(YOURBUTTONFUNCTION:) forControlEvents:UIControlEventTouchUpInside]; 


     [self.view addSubview:self.displayedInfoWindow]; 
    } 
} 




-(void)YOURBUTTONFUNCTION:(UIButton *)sender{ 
    NSLog(@"YOURBUTTONFUNCTION TAPPED"); 
} 








@end 
+0

'YOURBUTTONFUNCTION' - tên chức năng tốt nhất mà tôi từng thấy – fnc12

0
  1. Tạo một subview với nút trong đó.
  2. Gán khung thông tinChế độ xem cho khung phụ.

    [subView setFrame:infoview.frame]; 
    subView = [[[NSBundle mainBundle] loadNibNamed:<YourViewName> owner:self options:nil] objectAtIndex:0]; [self.mapview addSubview:subView]; 
    
0

Swift 3.0 Giải pháp

Đầu tiên thiết lập các đại biểu của MapView (GMSMapViewDelegate)

//empty the default infowindow 
     func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? { 
      return UIView() 
     } 

     // reset custom infowindow whenever marker is tapped 
     func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 

      customInfoView.removeFromSuperview() 
     // customInfoView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside) 
      self.view.addSubview(customInfoView) 

      // Remember to return false 
      // so marker event is still handled by delegate 
      return false 
     } 

     // let the custom infowindow follows the camera 
     func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { 
      if (locationMarker != nil){ 
       let location = locationMarker.position 
       customInfoView.center = mapView.projection.point(for: location) 
      } 
     } 

     // take care of the close event 
     func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { 
      customInfoView.removeFromSuperview() 
     } 

I have added customInfoWindow like this

và làm cho cửa hàng của quan điểm này (customInfoWindow) trong cùng một bộ điều khiển có mapView .

tôi đã có ý tưởng từ liên kết này nhờ nhà phát triển này Custom and interactive googlemaps(IOS SDK) infowindow

Các vấn đề liên quan