在Mapbox上添加模糊效果

5

我希望在Mapbox中添加模糊效果。当第一次加载地图时,只显示当前位置,其他地图为蓝色。请参见截图。

enter image description here

当我将用户位置移动到另一个位置时,地图上的其他位置会变得清晰可见。还会在特定位置显示注释。请参见截图。

enter image description here

我希望你能帮助我实现以下内容:

下面是一些实现代码:

- (CGPoint)convertLatLongCoord:(CGPoint)latLong {
    CGSize screenSize = [UIScreen mainScreen].applicationFrame.size];
    CGFloat SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
    CGFloat OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;
    CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;
    CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;

    return CGPointMake(x, y);
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    NSLog(@"data=%f",self.mapview.userLocation.coordinate.latitude);
    CLLocation *currentLoc=[locations objectAtIndex:0];
    _coordinate=currentLoc.coordinate;
CGPoint latLong = {_coordinate.latitude, _coordinate.longitude};
    oldcord = [self convertLatLongCoord:latLong];
    NSLog(@"Cartesian Coordinate: (%f, %f)",oldcord.x, oldcord.y);

}

在这段代码中,我将获取UserLocation坐标的完美视图像素点。然后,我想使用CGPoint清除视图中的模糊效果,但我无法获得它。我认为我将创建一个CGPoint数组,然后使用线条来清除视图的模糊效果,但我无法理解它,请给我建议。
我也尝试使用这个库,但我无法理解太多:https://github.com/DenHeadless/Shapes
1个回答

4
根据我的逻辑,我在这里添加了评论,我为您制作了一个样例,请查看下面的结果。样例包含以下逻辑:

enter image description here

  1. Using iOS Map, added a Custom UIView above the Map the class for the UIView is DrawView which actually handles all the drawing and erasing part.
  2. We are having CLLocationManager of course to determine user's movement.
  3. As soon as CLLocationManager start giving locations, we convert the coordinates with respect of MapView pin location using following code

    CGPoint pt=[self.map convertCoordinate:location.coordinate toPointToView:self.view];
    

    and we pass the CGPoint which is the converted value for the UIView to clear the area in drawing as

    [self.drawView eraseAtPoint:pt];
    
  4. When CGPoint from user's coordinate is passed to the DrawView eraseAtPoint function we erase an area of defined radius, and it clears out the area as wanted.

  5. I have added a sample directions.gpx file to simulate GPS location, the sample GIF shows the movement.

注意: 项目链接将在2017年3月14日后失效,请及时下载。如果需要再次获取样本,请在评论中提出请求。随意进行所需的修改。

更新:

添加了圆形区域擦除功能,只需将DrawView中的drawRect函数替换为以下代码即可:

- (void)drawRect:(CGRect)rect {    

    // Drawing code
    UIColor *backgroundColor=[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1.0f];//Grey

    [backgroundColor setFill];
    UIRectFill(rect);

    if(!self.initialLoad){//If the view has been loaded from next time we will try to clear area where required..

        // clear the background in the given rectangles
        for (NSValue *holeRectValue in _rectArray) {
            CGContextRef context = UIGraphicsGetCurrentContext();

            CGRect holeRect = [holeRectValue CGRectValue];

            [[UIColor clearColor] setFill];

            CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );

            CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor );
            CGContextSetBlendMode(context, kCGBlendModeClear);

            CGContextFillEllipseInRect( context, holeRectIntersection );

        }
    }

    self.initialLoad=NO;
}

干杯。

在 Mapbox 中,我会得到错误的 CGPoint(CGPoint)pt =(x = 1040634.6362283671,y = -389230.46748177585)。 - Jigar
@Jigar 我对 MapBox 一无所知,你可以将 iOS 地图保持在视图隐藏状态下并进行正确的计算,GPS 位置仍会在 MapBox 地图上移动,这只是一个想法。你可以随时深入研究 MapBox 类、教程和论坛以寻找答案,你的解决方案的基本思路已经在示例中了,这就是做事的方式。 - iphonic
我会尝试使用此代码创建圆形。我已经弄清楚了如何创建一个圆,但是仅限于一个圆而不是多个。我需要创建多个圆,而非矩形。如果您有任何想法,请告诉我。 - Jigar
@Jigar 是的,请查看我的更新,我已经添加了用圆形擦除区域的代码。 - iphonic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接