2

我正在使用最新的 GoogleMaps-iOS v1.3 SDK。

当在它后面有多个其他标记时,我无法让 didTapInfoWindowOfMarker 在信息窗口点击时触发。相反,点击信息窗口的频率往往就像点击它后面的标记(并打开不同的信息窗口)一样。

特别是,在我的地图上,我有几十个非常接近的 GMSMarker - 足够接近,以至于任何信息窗口弹出窗口都会覆盖多个标记,即使在最高缩放级别上也是如此。

有没有办法在点击信息窗口后面的标记之前强制点击信息窗口?

4

5 回答 5

3

供该线程的未来读者参考,此问题已在 2013 年 7 月的 1.4.0 版本中得到解决。在发行说明中,在已解决问题列表下,可以找到:

信息窗口不再允许点击通过它们

于 2013-07-26T18:24:22.063 回答
2

目前,我发现这是最好的解决方法:

https://github.com/ryanmaxwell/GoogleMapsCalloutView

基本上,在 -(UIView*)mapView:markerInfoWindow: 中返回一个大小为零的 UIView,因此没有可见的“谷歌地图信息窗口”,并借此机会同时在 MapView 顶部创建和添加自定义 UIView。在 mapView:didChangeCameraPosition 中,可以移动 UIView 以在地图移动时跟踪标记。

编辑:这还有一个好处,就是您可以拥有真正的交互式 UIView。(而且 SMCalloutView 看起来也比 googlemaps SDK 中的默认 infoWindow 好得多)

于 2013-06-12T10:58:05.773 回答
2

这可能不是最好的解决方案,它似乎有点倒退,特别是有很多标记,但它应该工作。

在您的地图视图委托中,将您的委托设置为:

-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker 
{
    for (int x = 0; x < myMapView.markers.count; x++) 
    { 
        GMSMarker * tempMarker = myMapView.markers[x]; 
        tempMarker.tappable = NO; 
    }
    mapView.selectedMarker = marker; 
    return YES; 
}

那么你必须设置另一个委托方法:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate 
{
    for (int x = 0; x < myMapView.markers.count; x++) 
    { 
        GMSMarker * tempMarker = myMapView.markers[x]; 
        tempMarker.tappable = YES; 
    } 
}

我设置了这个,如果用户点击屏幕外,它会关闭信息窗口并重新启用标记的“可点击性”。我知道这不是最好的方法,但是,鉴于您的情况,您可能别无选择。

我的建议是,如果您有过多的标记也在屏幕外,也许您可​​以将其设置为仅通过可见区域属性脱离屏幕标记(将其添加到您点击的标记委托中)

-(BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker
{
    GMSVisibleRegion myVisibleRegion = mapView.projection.visibleRegion;
    GMSCoordinateBounds * currentBounds = [[GMSCoordinateBounds alloc]initWithRegion:myVisibleRegion];
    for (int x = 0; x < myMapView.markers.count; x++) {
        if ([currentBounds containsCoordinate:marker.position]) {
            GMSMarker * tempMarker = myMapView.markers[x];
            tempMarker.tappable = NO;
        }
    }
    return NO;
}

然后只需获取这些范围内的标记。

如果您这样做,请在您的点击标记委托中,将其设置为

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{ 
    for (int x = 0; x < myMapView.markers.count; x++) {
        GMSMarker * tempMarker = myMapView.markers[x];
        if (tempMarker.tappable == NO) {
            tempMarker.tappable = YES;
        }
    }
}

这样,它只会更改当前不可点击的标记。

希望这可以帮助!

于 2013-05-29T15:22:59.147 回答
1

这是一种调整地图上所选标记位置以及禁用自定义视图后面的标记的方法。这些不是一种适合所有人的尺寸,您需要根据自己的规格进行调整。任何添加都非常欢迎。. .

请享用!

金牛座。. . 我无法确切地告诉您如何针对您的情况进行操作;但是,我可以给你我的代码来看看:

首先,我调整我的地图,使标记位于比 center 更理想的位置。. .

(需要注意,这需要一个北方方位,如果你允许用户旋转地图,你必须相应地调整)

您可以将调整要求存储在变量中并使用相同的要求,只要缩放保持不变

- (void) adjustDisplayForImage: (GMSMarker *) marker
{
    // GET AN OFFSET AMOUNT IN DEGREES IN ORDER TO PROPERLY PLACE THE MARKER ACCORDING TO ZOOM

    CGPoint topLeft = CGPointMake(0, 0);
    CGRect screenRect = [[UIScreen mainScreen]bounds];
    CGPoint lowerLeft = CGPointMake(0, screenRect.size.height);

    CLLocationCoordinate2D topLeftCoordinate = [myMapView.projection coordinateForPoint:topLeft];
    CLLocationCoordinate2D lowerLeftCoordinate = [myMapView.projection coordinateForPoint:lowerLeft];

    CGFloat screenDistanceInDegreesLatitude = topLeftCoordinate.latitude - lowerLeftCoordinate.latitude;
    CGFloat adjustmentDistanceInDegreesLatitude = screenDistanceInDegreesLatitude / 2.8;
    // this is the amount that the new center needs to be offset in order to orient marker on screen

    // these are CLLocationDegrees variables, declared elsewhere
    adjustedLatitudeDegrees = marker.position.latitude + adjustmentDistanceInDegreesLatitude;

    adjustedLatitudeDegrees = marker.position.latitude + adjustmentDistanceInDegreesLatitude;

    CLLocationCoordinate2D newCenterCoordinate = CLLocationCoordinate2DMake(adjustedLatitudeDegrees, marker.position.longitude);

    myMapView.selectedMarker = marker;
    [myMapView animateToLocation:newCenterCoordinate];

    // I'm saving the current zoom so I don't have to get my adjustment distances again unless zoom changes
    lastZoom = myMapView.zoom;
}

好的,现在我有了这些信息,让我们删除视图后面的标记。. .

- (void) disableMarkersBehindView
{
    // my marker window is 213 x 280

    int markerWindowHeight = 280;
    int markerWindowWidth = 213;
    int approximateMarkerHeight = 10;

    // I'm just guessing here for marker height, whatever your marker's height is, use that

    // because of my adjustment, the marker position is adjusted about down 35% from the middle

    CGFloat adjustedDistanceInCGPoints =  myMapView.frame.size.height * (1 / 2.7);

    // This is because the marker defaults to the middle of the screen
    CGFloat middleY = myMapView.frame.size.height / 2;
    CGFloat newMarkerPositionY = middleY + adjustedDistanceInCGPoints;

    CGFloat topOfMarkerWindowY = newMarkerPositionY - markerWindowHeight - approximateMarkerHeight;

    // now we need a NorthEast and a SouthWest coordinate

    // NORTHEAST

    CGFloat halfWidth = myMapView.frame.size.width / 2;
    CGFloat northEastX = halfWidth - (markerWindowWidth / 2);

    CGPoint northEastCGPoint = CGPointMake(northEastX, topOfMarkerWindowY);

    CLLocationCoordinate2D northEastCoordinate = [myMapView.projection coordinateForPoint:northEastCGPoint];

    // SOUTHWEST

    CGFloat southWestX = halfWidth + (markerWindowWidth / 2);
    CGPoint southWestCGPoint = CGPointMake(southWestX, newMarkerPositionY);

    CLLocationCoordinate2D southWestCoordinate = [myMapView.projection coordinateForPoint:southWestCGPoint];

    // Now, make a boundary

    GMSCoordinateBounds * myViewBounds = [[GMSCoordinateBounds alloc]initWithCoordinate: northEastCoordinate coordinate:southWestCoordinate];

    for (int x = 0 ; x < myMapView.markers.count ; x++) {

        GMSMarker * marker = myMapView.markers[x];
        if ([myViewBounds containsCoordinate:marker.position]) {
            marker.tappable = NO;
            // here maybe add the markers to a seperate array so you can quickly make them tappable again wherever you need to
        }
    }
}

这考虑了各种缩放级别,如果您限制地图上的缩放,您可能会简化它。

请记住让您的标记再次可点击,这就是为什么我建议将它们保存在单独的数组中

并且不要忘记,调整仅基于 Northern 轴承,要禁用旋转轴承,请使用

myMapView.settings.rotateGestures = NO;

如果没有,请务必先将地图重新​​定向到北方或做一些数学运算!

- 希望这可以帮助!

于 2013-06-15T03:06:43.517 回答
0

我就是这样做的。设置 mapview.delegate = self

你的 .h 文件

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

@interface MapViewController : UIViewController <CLLocationManagerDelegate, GMSMapViewDelegate>

@end

.m 文件的 viewDidLoad

    - (void)viewDidLoad {
        [super viewDidLoad];

        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.124291
                                                                longitude:-104.765625
                                                                     zoom:2];
        mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
        mapView_.myLocationEnabled = YES;
        self.view = mapView_;
        mapView_.delegate = self;
    }

#pragma mark - set current location
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

}

#pragma mark - mapview events
-(BOOL) mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker{
    NSLog(@"%@", marker.description);
//    show info window
    [mapView_ setSelectedMarker:marker];
    return YES;
}

-(void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker{
    NSLog(@"info window tapped");
}
于 2014-11-16T22:19:46.070 回答