【问题标题】:How to display a circle in GMSMapView如何在 GMSMapView 中显示一个圆圈
【发布时间】:2013-03-01 02:24:17
【问题描述】:

我需要在 GMSMapView 上显示一个圆圈(与 MKCircle 一样)。这在使用 MKMapView 和 MKCircle 时很容易,但不能将 MKCircle 与 GMSMapView 一起使用。 有任何想法吗?

更新:
这是当前 (18.03.2013) 选项:
1. 包含圆形图像的地面标记。
2. 一个由几段组成的圆(折线)。

编辑:
3. Google 添加了 GMSCircle (23.04.2013)

 GMSGroundOverlayOptions *overlayOptions = [GMSGroundOverlayOptions options];
    overlayOptions.icon = [UIImage imageNamed:@"circle"];
    overlayOptions.position = touchMapCoordinate;
    overlayOptions.bearing = 0;
    overlayOptions.zoomLevel = 14.3;
 [mapView addGroundOverlayWithOptions:overlayOptions];

对于 40x40 像素的圆形图像,它看起来不错。 (半径约为 100 m)

小分段路径解决方案:

    GMSPolylineOptions *circle = [GMSPolylineOptions options];
    CGPoint touchPoint = [mapView.projection pointForCoordinate:touchMapCoordinate];
    GMSMutablePath *path = [GMSMutablePath path];

    CGPoint circlePoint;

    for (int i = 0; i < 360; i++)
    { 
        circlePoint.x = touchPoint.x + radius * cos(i*M_PI/180);
        circlePoint.y = touchPoint.y + radius * sin(i*M_PI/180);

        CLLocationCoordinate2D aux = [mapView.projection coordinateForPoint:circlePoint];
        [path addCoordinate:aux];
    }

    circle.path = path;
    circle.width = 1;

    [mapView addPolylineWithOptions:circle];

编辑:08.05.2013

GMSCircle 解决方案:

     CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(latitude, longitude);
     GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
                                     radius:1000];

     circ.fillColor = [UIColor blueColor];
     circ.strokeColor = [UIColor redColor];
     circ.strokeWidth = 5;
     circ.map = mapView;

【问题讨论】:

  • 你能公布你到目前为止的尝试吗?
  • 我什至不知道如何开始。我考虑过使用 GMSGroundOverlay 和 GMSGroundOverlayOptions 添加一个圆形图标,但这意味着该圆形始终具有特定的半径。
  • 它不适用于 iOS 8

标签: ios google-maps google-maps-sdk-ios


【解决方案1】:

这个问题已经有一年多了,所以有点晚了,但是谷歌搜索把我带到了这里,所以我想我会更新这个。后人4TW!

据我所知,现在有一个 GMSCircle 可以做几乎所有 MKCircle 可以做的事情。
Google's documentation on the GMSCircle

// Build a circle for the GMSMapView
GMSCircle *geoFenceCircle = [[GMSCircle alloc] init];
geoFenceCircle.radius = 130; // Meters
geoFenceCircle.position = SOME_CLLOCATION.coordinate; // Some CLLocationCoordinate2D position
geoFenceCircle.fillColor = [UIColor colorWithWhite:0.7 alpha:0.5];
geoFenceCircle.strokeWidth = 3;
geoFenceCircle.strokeColor = [UIColor orangeColor];
geoFenceCircle.map = mapView; // Add it to the map.

//为 Swift 5.3 更新代码

 let circle = GMSCircle(position: position, radius:10)
 circle.fillColor = .clear
 circle.strokeWidth = 3
 circle.strokeColor = .black
 circle.map = mapView

它的行为与 MKCircle(叠加)非常相似,因为它会根据地图的缩放级别等调整大小。请忽略中心的蓝色圆圈;那是地图视图上显示的用户位置,我只是将相同的坐标用于 GMSCircle 的中心点。

超级简单。查看图片:

一个缩放级别:

在这里,我们缩小了一点:

【讨论】:

  • 是否可以在圆圈上绘制文字?
  • GMSOverlays 有一个 'title' 属性,可用于将文本添加到某些标记类型。如果这不能让你得到你想要的,这是我过去成功做过的事情,你可以继承 GMSMarker 并将图标更改为你想要的文本(你必须创建一个图像出来它,我记得)。然后,您可以将其放置在与您的圈子相同的位置(或将圈子添加为 GMSMarker 子类中的成员)。 developers.google.com/maps/documentation/ios-sdk/reference/…
  • 嘿,我想在地图上修复圆圈并获取圆圈内的位置,用户可以移动地图,但圆圈将保持在同一位置有什么帮助吗?谢谢 - Kuldeep @u2Fan
  • 以上内容将完全按照您的意愿进行,而不是“获得圈内的位置”。我不确定你的意思。您想要圆圈内的地理位置(坐标)还是 POI(兴趣点,如餐馆)? @dreamBegin
  • 我只想获取圆的区域,我将有一个包含 lat/long 的响应字符串我想检查 lat/long 是否在该区域内,圆也应该不用缩放来展开或折叠,谢谢 - kuldeep @u2Fan
【解决方案2】:

目前SDK不支持圈子,但是这里有一个添加圈子的功能请求:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=4971

与此同时,您也许可以通过绘制一条多段线来假造一个圆?

【讨论】:

    【解决方案3】:
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        // Make sure cicle is a GMSCircle Object  and it is a class variable
        circle.map = nil
    
        let point = mapView.center
        circle.position = mapView.projection.coordinate(for: point)
        circle.radius = 130.0
        circle.fillColor = .clear
        circle.strokeColor = .black
        circle.strokeWidth = 3.4
        circle.map = mapView
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2012-11-12
      • 1970-01-01
      • 2021-05-16
      • 2018-06-17
      • 2018-12-15
      • 1970-01-01
      • 2019-07-19
      • 2020-08-09
      相关资源
      最近更新 更多