代码之家  ›  专栏  ›  技术社区  ›  Bogus

如何在GMSMapView中显示圆

  •  13
  • Bogus  · 技术社区  · 11 年前

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

    更新:
    这是当前(2013年3月18日)的选项:
    1.一种包含圆形图像的地面标记。
    2.由几个线段(多段线)组成的圆。

    编辑:
    3.谷歌增加了GMSCircle(2013年4月23日)

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

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

    小型分段路径解决方案:

        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];
    

    编辑:2013年5月8日

    GMS环形解决方案:

         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;
    
    3 回复  |  直到 10 年前
        1
  •  13
  •   Ashutosh Shukla    4 年前

    这个问题已经一年多了,现在有点晚了,但谷歌搜索把我带到了这里,所以我想我应该更新一下。后部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的中心点。

    超级简单。查看图片:

    一个缩放级别: enter image description here

    在这里,我们缩小了一点: enter image description here

        2
  •  1
  •   Saxon Druce    11 年前

    目前SDK不支持圆圈,但有一个功能请求需要在此处添加圆圈:

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

    与此同时,你可以通过画一条多段线来伪造一个圆,其中有几个短线段?

        3
  •  0
  •   Rajasekhar Pasupuleti    6 年前
    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
    }