我试图在MKPolygon 4.0的MKMapView上绘制一个iOS。我有一个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;
}我的理解是:
MKPolygonView.
我的问题是如何将包含在NSArray (coordinateData)中的自定义对象转换为CLLocationCoordinate2d数组,以便多边形能够解释和呈现?我不知道CLLocationCoordinate2d怎么会是一个数组?有人能澄清一下这个问题吗。
发布于 2011-03-29 15:07:08
polygonWithCoordinates方法需要一个C数组的CLLocationCoordinate2D结构。您可以使用malloc为数组分配内存( free用于释放内存)。循环遍历NSArray并将其设置为struct数组中的每个元素。
例如:
coordsLen = [coordinateData count];
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * coordsLen);
for (int i=0; i < coordsLen; i++)
{
YourCustomObj *coordObj = (YourCustomObj *)[coordinateData objectAtIndex:i];
coords[i] = CLLocationCoordinate2DMake(coordObj.latitude, coordObj.longitude);
}
MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:coordsLen];
free(coords);
[mapView addOverlay:polygon];viewForOverlay方法应该如下所示:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
polygonView.lineWidth = 1.0;
polygonView.strokeColor = [UIColor redColor];
polygonView.fillColor = [UIColor greenColor];
return polygonView;
}发布于 2014-11-12 11:40:46
对于iOS 7.0及更高版本,我们应该使用MKPolygonRenderer而不是MKPolygonView,
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolygonRenderer * polygonView = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
polygonView.fillColor = [UIColor greenColor];
polygonView.strokeColor = [UIColor redColor] ;
polygonView.lineWidth = 1.0;
return polygonView;
}发布于 2019-06-18 10:46:54
Swift 4中的码
在json中的坐标:
{
"coordinates": [
[-73.947676,40.660297],
[-73.947264,40.656437],
[-73.947159,40.655594],
[-73.946479,40.6491],
[-73.947467,40.649039]
}读取坐标:
let coordinates = json["coordinates"] as! [[Double]] 创建点数组:
var locationCoordinates = [CLLocationCoordinate2D]()
for coordinate in coordinates{
locationCoordinates.append(CLLocationCoordinate2DMake(coordinate.last!, coordinate.first!))
}创建一个多边形并将其添加到地图中:
map.addOverlay(MKPolyline(coordinates: locationCoordinates,
count: locationCoordinates.count))确保您的VC与MKMapViewDelegate对抗
class ViewController: UIViewController, MKMapViewDelegate { ... }并添加此方法:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
polygonView.fillColor = .black
polygonView.strokeColor = .red
polygonView.lineWidth = 2.0
return polygonView
return MKOverlayRenderer()
}https://stackoverflow.com/questions/5474299
复制相似问题