15

einfache FrageHinzufügen von Click-Ereignis auf Infofenster/Marker in Google Maps SDK für native iOS/Objective C

Ich arbeitet an einer App für iOS, wo ich den neuen Google-Map für native iOS eingebettet. Alles funktioniert gut bis auf ein Problem, wo ich weder hier noch bei Google eine passende Lösung für natives IOS/Objective-C finden kann (wenn es eine gibt, bitte entschuldige mich für die Störung)

Was ich will: Ich möchte, dass der Benutzer auf den Marker klickt, der das Infofenster öffnet. Nach Wenn er auf das Info-Fenster klickt oder klickt, sollte es eine neue UIView mit weiteren Informationen öffnen.

Wie kann ich das tun?

Vielen Dank für Ratschläge

+0

Es Annotation Disclosure genannt: http: // stackoverflow.com/questions/1433067/adding-an-accessory-view-button-to-the-mkmapview-call-out-annotation – viral

+5

die akzeptierte Antwort ist relevant für Apple MapKit, nicht Google Maps ?! –

+0

ja du hast Recht. Und es ist eine alte Antwort mit einer alten Frage sowieso: D – Corona

Antwort

76

1.conform dem Protokoll GMSMapViewDelegate verwenden.

@interface YourViewController() <GMSMapViewDelegate> 
// your properties 
@end 

2. Setzen Sie Ihren mapView_ Delegierten.

mapView_.delegate = self; 

3.implement die GMSMapViewDelegate Methode

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

BTW, marker.userData nützlich. Sie können Ihre benötigten Daten darin eingeben und sie in - mapView:didTapInfoWindowOfMarker:

+0

klare und konsistente Antwort. – andi

+2

Sehr cool, dass Sie die Verwendung von userData gezeigt haben. Ich bin ein Neuling für iOS und Map SDK und wusste nicht, wie man userData verwenden kann. Jetzt weiß ich. Vielen Dank! (Meine Fallstudie: Ich setze auf userdata url, um die Detailansicht des Objekts von meinem REST-Service zu zeigen, die Quelle von Markern ist, mit der in weiteren Ansichten gearbeitet werden soll.) – andi

+0

Dies sollte die ausgewählte Antwort sein. Danke @Brian –

2

wo Sie Karte hinzufügen, hinzufügen

mapView_.delegate=self; 

dann diese

-(void)mapView:(GMSMapView *)mapView 
didTapInfoWindowOfMarker:(id<GMSMarker>)marker{ 

    //info window tapped 

} 
1

verwenden Ich verwende Google Maps SDK für iOS.

Ich habe uiview subclassiert, um eine benutzerdefinierte Ansicht "InfoWindow" für Infowindow zu erstellen.

@property hinzufügen (nicht atomisch, beibehalten) UIView * actionOverlayCalloutView; in der Headerdatei Ihres Viewcontrollers.

- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker { 
if(self.objSelectedParking != nil) 
{ 
    float anchorSize = 0.5f; 
    float infoWindowWidth = 250.0f; 
    float infoWindowHeight = 250.0f; 

    [self.actionOverlayCalloutView removeFromSuperview]; 
    InfoWindow *infoWindow = [[InfoWindow alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight)]; 
    infoWindow.lblTitle.text = self.objSelectedParking.strParkingName; 
    infoWindow.lblDescription.text = self.objSelectedParking.strParkingDescription; 
    infoWindow.lblAddress.text = self.objSelectedParking.strParkingAddress; 
    infoWindow.lblPhone.text = self.objSelectedParking.strParkingPhone; 
    infoWindow.imageViewParking.image = [UIImage imageNamed:@"parking_image_sample.jpg"]; 

    float offset = anchorSize * M_SQRT2; 

    self.actionOverlayCalloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight - offset/2)]; 
    [self.actionOverlayCalloutView setBackgroundColor:[UIColor clearColor]]; 

    self.actionOverlayCalloutView.layer.cornerRadius = 5; 
    self.actionOverlayCalloutView.layer.masksToBounds = YES; 

    UIButton *hiddenCloseButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.viewContainer.frame.origin.x + infoWindow.viewContainer.frame.size.width - 30), 10, 20, 20)]; 
    [hiddenCloseButton addTarget:self action:@selector(hiddenCloseButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.actionOverlayCalloutView addSubview:hiddenCloseButton]; 

    UIButton *hiddenDirectionButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.lblAddress.frame.origin.x + infoWindow.lblAddress.frame.size.width + 5), (infoWindow.lblAddress.frame.origin.y - 15), 25, 25)]; 
    [hiddenDirectionButton addTarget:self action:@selector(hiddenDirectionButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.actionOverlayCalloutView addSubview:hiddenDirectionButton]; 

    UIButton *hiddenInfoButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.innerContainerView.frame.origin.x + infoWindow.imageViewParking.frame.origin.x + infoWindow.imageViewParking.frame.size.width + 20), (infoWindow.innerContainerView.frame.origin.y + 25), 25, 25)]; 
    [hiddenInfoButton addTarget:self action:@selector(hiddenInfoButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.actionOverlayCalloutView addSubview:hiddenInfoButton]; 

    UIButton *hiddenScheduleButton = [[UIButton alloc] initWithFrame:CGRectMake((infoWindow.innerContainerView.frame.origin.x + infoWindow.verticalLineSeperatorView.frame.origin.x + infoWindow.verticalLineSeperatorView.frame.size.width + 10), (infoWindow.innerContainerView.frame.origin.y + 25), 25, 25)]; 
    [hiddenScheduleButton addTarget:self action:@selector(hiddenScheduleButtonClickedInInfowindow:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.actionOverlayCalloutView addSubview:hiddenScheduleButton]; 

    [infoWindow addSubview:self.actionOverlayCalloutView]; 

    CLLocationCoordinate2D anchor = [_mapView.selectedMarker position]; 
    CGPoint point = [_mapView.projection pointForCoordinate:anchor]; 
    point.y -= _mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; 
    self.actionOverlayCalloutView.center = point; 

    [_mapView addSubview:self.actionOverlayCalloutView]; 
    return infoWindow; 
} 

return nil; 
} 

-(void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position { 
if (pMapView.selectedMarker != nil && self.actionOverlayCalloutView.superview) 
{ 
    float anchorSize = 0.5f; 
    float infoWindowHeight = 250.0f; 

    CLLocationCoordinate2D anchor = [_mapView.selectedMarker position]; 
    CGPoint point = [_mapView.projection pointForCoordinate:anchor]; 
    float offset = anchorSize * M_SQRT2; 
    point.y -= _mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2; 
    point.y = point.y - 10; //PRATIK GUJARATI CODE TO ADJUST HEIGHT AND Y VALUE OF TRANSPARENT OVERLAY VIEW 
    self.actionOverlayCalloutView.center = point; 
} else { 
    [self.actionOverlayCalloutView removeFromSuperview]; 
} 
} 

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate { 
[self.actionOverlayCalloutView removeFromSuperview]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
if ([keyPath isEqualToString:@"mapView.selectedMarker"]) { 
    if (!_mapView.selectedMarker) { 
     [self.actionOverlayCalloutView removeFromSuperview]; 
    } 
} 
} 
0
  1. Erstellen Sie eine Subview, die Sie in Infowindow zeigen möchten.

  2. Set-Frame der Subview entspricht dem Frame der InfoWindow-Ansicht.

    subView = [[[NSBundle mainBundle] loadNibNamed:@"viewName" owner:self options:nil] objectAtIndex:0]; 
    [subView setFrame:infoview.frame]; 
    [self.mapview addSubview:subView]; 
    
0

erhalten Informationen bei google Karte anzeigen Informationen

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) { 
print(marker.title!) 
print(marker.snippet!) 

}

0

eine vollständige Antwort für eine schnelle 4

  1. hinzufügen GMSMapViewDelegate als Delegierter

  2. set yourmap Delegate wie folgt: googlemap.Delegierter = Selbst

  3. fügen Sie diese

    func mapView Func (_ mapView: GMSMapView, didTap Marker: GMSMarker) -> Bool {

    // do something 
    
        return true 
    
    } 
    
Verwandte Themen