我正在开发一个带有地图的应用程序,用户可以在其中绘制多边形区域.
我的问题是可以用结绘制多边形(见图)(我不知道结是否是正确的词).我没有找到阻止多边形得到结的简单方法.对于附加图像的情况,我希望移除小卷曲,甚至要平滑轮廓
你知道一种方法吗?
绘制多边形当用户触摸屏幕的过程中,确实使用MKPolyline
,MKPolygon
并且MKOverlay
如下所示:
- (void)touchesBegan:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesMoved:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesEnded:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
[self didTouchUpInsideDrawButton:nil];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayPathView *overlayPathView;
if ([overlay isKindOfClass:[MKPolygon …
Run Code Online (Sandbox Code Playgroud) 我试图在iOS 4.0中的MKMapView上绘制MKPolygon.我有一个NSArray,其中包含自定义对象,包括纬度/经度属性.我有一个代码示例如下:
- (void)viewDidLoad {
[super viewDidLoad];
dataController = [[DataController alloc] initWithMockData];
coordinateData = [dataController getCordData];
CLLocationCoordinate2D *coords = NULL;
NSUInteger coordsLen = 0;
/* How do we actually define an array of CLLocationCoordinate2d? */
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
[mapView addOverlay: polygon];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolygonView *polygonView = [[MKPolygonView alloc] initWithPolygon: routePolygon];
NSLog(@"Attempting to add Overlay View");
return polygonView;
}
Run Code Online (Sandbox Code Playgroud)
我理解的方式是:
我的问题是如何获取NSArray(coordinateData)中包含的自定义对象并将这些对象转换为CLLocationCoordinate2d数组,以便Polygon可以解释和呈现?我不确定CLLocationCoordinate2d甚至是一个数组?有人可以对此有所清晰.
此时,我正在试图弄清楚MKMapView上的坐标是否在提前绘制出的纬度/经度的MKPolygon内.
我正在使用CGPathContainsPoint来确定坐标是否在地图上的多边形内,但无论我选择哪个坐标,它总是返回false.
任何人都可以解释究竟出了什么问题?下面是我在Swift中的代码.
class ViewController: UIViewController, MKMapViewDelegate
@IBOutlet weak var mapView: MKMapView!
let initialLocation = CLLocation(latitude: 43.656734, longitude: -79.381576)
let point = CGPointMake(43.656734, -79.381576)
let regionRadius: CLLocationDistance = 500
let point1 = CLLocationCoordinate2D(latitude: 43.656734, longitude: -79.381576)
var points = [CLLocationCoordinate2DMake(43.655782, -79.382094),
CLLocationCoordinate2DMake(43.657499, -79.382310),
CLLocationCoordinate2DMake(43.656656, -79.380497),
CLLocationCoordinate2DMake(43.655782, -79.382094)]
override func viewDidLoad() {
super.viewDidLoad()
centerMapOnLocation(initialLocation)
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
var annotation = MKPointAnnotation()
annotation.coordinate = point1
annotation.title = "Test"
annotation.subtitle = "Test"
mapView.addAnnotation(annotation)
self.mapView.delegate = self
}
func …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为MKPolygon制作面积计算类别.我发现了一些JS代码https://github.com/mapbox/geojson-area/blob/master/index.js#L1一个链接的算法:http://trs-new.jpl.nasa.gov/dspace/handle/2014/40409.它说:
这是我的代码,它给出了错误的结果(比实际数千倍):
#define kEarthRadius 6378137
@implementation MKPolygon (AreaCalculation)
- (double) area {
double area = 0;
NSArray *coords = [self coordinates];
if (coords.count > 2) {
CLLocationCoordinate2D p1, p2;
for (int i = 0; i < coords.count - 1; i++) {
p1 = [coords[i] MKCoordinateValue];
p2 = [coords[i + 1] MKCoordinateValue];
area += degreesToRadians(p2.longitude - p1.longitude) * (2 + sinf(degreesToRadians(p1.latitude)) + sinf(degreesToRadians(p2.latitude)));
}
area = area * kEarthRadius * kEarthRadius / 2;
}
return area;
} …
Run Code Online (Sandbox Code Playgroud) 似乎有一个覆盖和"问题" MapKit
.与注释不同,叠加层不会被重用,因此在添加多个叠加层时会导致真实设备出现内存问题.我多次遇到过这个问题.所以我的问题是,我如何重用MKOverlay,从而提高叠加的性能MapKit
?
基于我在这个SO问题上找到的内容(在MKMapView的叠加上触摸事件),我已经实现了一种拦截MKPolygon上的轻击手势的方法.
它在我们的应用程序中运行良好,该应用程序是使用Xcode 4.6.3针对iOS 6构建的.但是当我在iOS 7设备上尝试它时停止工作.
特别
CLLocationCoordinate2D coord = [neighborhoodMap_ convertPoint:point
toCoordinateFromView:neighborhoodMap_];
// We get view from MKMapView's viewForOverlay.
MKPolygonView *polygonView = (MKPolygonView*) view;
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path,
NULL,
polygonViewPoint,
NO);
Run Code Online (Sandbox Code Playgroud)
由于某种原因,即使给定的坐标在MKPolygonView中,对CGPathContainsPoint的调用也不再返回YES.不确定是否有人遇到过这个问题,但我很感激您的任何见解.
谢谢!
我有一个自定义的多边形对象,所以我可以将地图叠加保存到Realm.我能够成功创建这个对象,但是当我想要检索var多边形对象时,它返回nil.当我打印多边形对象时,它会将所有数据打印出来.
这是它打印出来的样本.
CustomPolygon {
name = Polygon1;
id = p1;
polygon = Polygon {
coordinates = RLMArray <0x7f928ef36230> (
[0] Coordinate {
latitude = -36.914167;
longitude = 174.904722;
},
[1] Coordinate {
latitude = -36.9325;
longitude = 174.957222;
}, . . .
);
};
}
Run Code Online (Sandbox Code Playgroud)
我的函数从Realm加载数据
func loadOverlaysFromRealm(){
do {
let realm = try Realm()
let polygons = realm.objects(CustomPolygon)
for p in polygons {
var coordinates = [CLLocationCoordinate2D]()
print(p) // !!!!! prints out what is above
print(p.polygon) // !!!!! Returns …
Run Code Online (Sandbox Code Playgroud) 我正在寻找测试MKPolygon是否与MKCircle相交的一些指导.目前我正在使用:
if ([circle intersectsMapRect:[poly boundingMapRect]]) {
//they do intersect
}
Run Code Online (Sandbox Code Playgroud)
我发现这返回不准确的结果只是b/c它在我的圆圈周围绘制一个矩形,从而给我交叉,否则不应该.
搜索主题让我看到了Chad Saxon的多边形 - 多边形交叉项目.如果我能以某种方式将我的MKCircle转换为多边形多边形,这可能是有用的 - 这可能是可能的,但最终我相信这是解决这个问题的圆形方法.
我最终想知道在钻研我自己的自定义几何光线测试算法实现之前是否有一个我忽略的简单解决方案.
各位开发者,我正在尝试在mapview上实现多边形叠加,如下所示:
private func drawOverlayForObject(object: MyStruct) {
if let coordinates: [CLLocationCoordinate2D] = object.geometry?.coordinates {
let polygon = MKPolygon(coordinates: coordinates, count: coordinates.count)
self.mapView.addOverlay(polygon)
}
}
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
在调用中缺少参数'interiorPolygons'的参数
根据文件: Apple Docu:
可变指针
当函数声明为接受UnsafeMutablePointer参数时,它可以接受以下任何一项:
- nil,作为空指针传递
- UnsafeMutablePointer值
- 一个输入输出表达式,其操作数是类型为Type的存储左值,它作为左值的地址传递
- 输入输出[类型]值,作为指向数组开头的指针传递,并在调用期间延长寿命
现在我认为我的方法是正确的,提供[CLLocationCoordinate2D]数组.有没有人遇到同样的问题,并找到了解决方法?
谢谢罗尼
我正在使用iOS Mapbox
SDK,并且需要在多边形中找到中心坐标,因为我想在中心坐标中添加标记。如何在Swift中执行此操作?
func drawPolygonFeature(shapes: [MGLShape & MGLFeature]) {
let shapeSource = MGLShapeSource(identifier: "MultiPolygonShapeSource", shapes: shapes, options: nil)
let lineStyleLayer = MGLLineStyleLayer(identifier: "LineStyleLayer", source: shapeSource)
lineStyleLayer.lineColor = NSExpression(forConstantValue: UIColor.purple)
lineStyleLayer.lineOpacity = NSExpression(forConstantValue: 0.5)
lineStyleLayer.lineWidth = NSExpression(forConstantValue: 4)
DispatchQueue.main.async(execute: {[weak self] in
guard let self = self else { return }
self.mapView.style?.addSource(shapeSource)
self.mapView.style?.addLayer(lineStyleLayer)
let multiPolygonFeature = shapes.first as? MGLMultiPolygonFeature
if let centerCoordinate = multiPolygonFeature?.polygons.first?.coordinate {
self.mapView.centerCoordinate = centerCoordinate
// but centerCoordinate var does not contain the center coordinate
}
})
}
Run Code Online (Sandbox Code Playgroud)