我试图找出一种方法来获取iOS应用程序上MKMapView上绘制的MKPolyline的所有纬度和经度点.
我知道MKPolyline不存储纬度和经度点,但我正在寻找一种方法来构建一个lat和long数组,MKPolyline会在地图上触摸.
有人在这方面有具体的解决方案吗?
谢谢
编辑:看到第一个响应(谢谢)后,我想我需要更好地解释我的代码在做什么:
就这样.
所以,我已经为我建了一条折线.所以我想以某种方式得到折线中找到的所有点并将它们映射到纬度和长度......
在iPhone上内置Maps.app上显示方向时,您可以"选择"通过点击它显示的通常3个路线选项之一.我不想复制这个功能并检查一个tap是否在给定的MKPolyline中.
目前我检测到MapView上的点击如下:
// Add Gesture Recognizer to MapView to detect taps
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapTap:)];
// we require all gesture recognizer except other single-tap gesture recognizers to fail
for (UIGestureRecognizer *gesture in self.gestureRecognizers) {
if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
UITapGestureRecognizer *systemTap = (UITapGestureRecognizer *)gesture;
if (systemTap.numberOfTapsRequired > 1) {
[tap requireGestureRecognizerToFail:systemTap];
}
} else {
[tap requireGestureRecognizerToFail:gesture];
}
}
[self addGestureRecognizer:tap];
Run Code Online (Sandbox Code Playgroud)
我按如下方式处理水龙头:
- (void)handleMapTap:(UITapGestureRecognizer *)tap {
if ((tap.state & UIGestureRecognizerStateRecognized) == UIGestureRecognizerStateRecognized) {
// Check if the …
Run Code Online (Sandbox Code Playgroud) 我如何与swift中用于获取大小的C数组的函数进行交互?
我阅读了与C APIS的交互,但仍然无法弄清楚这一点.
func getCoordinates(_ coords:UnsafeMutablePointer<CLLocationCoordinate2D>,range range: NSRange)
状态的coords参数的文档:"在输入时,您必须提供足够大的C数组结构以容纳所需数量的坐标.在输出时,此结构包含请求的坐标数据."
我尝试了几件事,最近一次:
var coordinates: UnsafeMutablePointer<CLLocationCoordinate2D> = nil
polyline.getCoordinates(&coordinates, range: NSMakeRange(0, polyline.pointCount))
Run Code Online (Sandbox Code Playgroud)
我是否必须使用以下内容:
var coordinates = UnsafeMutablePointer<CLLocationCoordinate2D>(calloc(1, UInt(polyline.pointCount)))
Run Code Online (Sandbox Code Playgroud)
把头发拉出来......有什么想法吗?
我最近开始学习objectiveC并开始在iOS6中开发应用程序.
现在,我正在尝试将其转换为iOS7并面临MKMap的问题.
在iOS6中,我使用的是viewForOverlay.
在iOS7中,我将其更改为renderForOverlay.但是,我的应用程序不是调用mapView:rendererForOverlay.以下是我的代码.感谢您的帮助.
- (void) drawPolyline:(NSArray *)locations
{
[mapView setDelegate:self];
...
...
self.polyline = [MKPolyline polylineWithCoordinates:locationCoordinate2DArray count:numberOfLocations];
free(locationCoordinate2DArray);
[mapView addOverlay:self.polyline];
[mapView setNeedsDisplay];
}
- (MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
MKPolylineRenderer* lineView = [[MKPolylineRenderer alloc] initWithPolyline:self.polyline];
lineView.strokeColor = [UIColor blueColor];
lineView.lineWidth = 7;
return lineView;
}
Run Code Online (Sandbox Code Playgroud) 我可以使用下面的代码(无论如何它的一部分)在两点之间创建一条线,我怎样才能使线条点缀而不是实线?也可以在线条越长的时候改变不透明度?
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay
{
MKPolylineRenderer *renderer =[[MKPolylineRenderer alloc] initWithPolyline:overlay];
renderer.strokeColor = [UIColor orangeColor];
renderer.lineWidth = 3.0;
return renderer;
}
Run Code Online (Sandbox Code Playgroud) 我认为iOS 7 MKMapSnapshotter
是一种拍摄快照的简单方法MKMapView
,其好处是可以在不将地图加载到视图中的情况下完成.即使添加引脚和覆盖图似乎更多的工作(因为需要核心图形).WWDC视频提供了一个非常好的例子来创建一个MKMapSnapshotter
添加MKAnnotationView
.
然而,对于没有很多核心图形经验的人来说,如何创建一个MKMapSnapshotter
来自的人并不是很明显MKPolylineRenderer
.
我试过这样做,但路径不准确.它准确地绘制了大约10%的线,但随后直接绘制了剩下的路径.
这是我的代码:
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:snapshotOptions];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error)
{
if (error == nil)
{
UIImage *mapSnapshot = [snapshot image];
UIGraphicsBeginImageContextWithOptions(mapSnapshot.size,
YES,
mapSnapshot.scale);
[mapSnapshot drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw the points from the MKPolylineRenderer in core graphics for mapsnapshotter...
MKPolylineRenderer *overlay = (MKPolylineRenderer *)[self.mapView rendererForOverlay:[_mapView.overlays lastObject]];
if (overlay.path)
{
CGFloat zoomScale = 3.0;
[overlay applyStrokePropertiesToContext:context
atZoomScale:zoomScale];
CGContextAddPath(context, …
Run Code Online (Sandbox Code Playgroud) 我想在我的Swift应用程序中绘制折线.
SWIFT代码
class MapViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var theMapView: MKMapView
override func viewDidLoad() {
super.viewDidLoad()
setMapView()
}
func setMapView() {
//theMapView.zoomEnabled = false
//theMapView.scrollEnabled = false
theMapView.rotateEnabled = false
//
var lat: CLLocationDegrees = 37.586601
var lng: CLLocationDegrees = 127.009381
//
var latDelta: CLLocationDegrees = 0.008
var lngDelta: CLLocationDegrees = 0.008
var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lngDelta)
var initLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng)
var theRegion: MKCoordinateRegion = MKCoordinateRegionMake(initLocation, theSpan)
self.theMapView.setRegion(theRegion, animated: true)
var locations = [CLLocation(latitude: 37.582691, longitude: 127.011186), …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个带有地图的应用程序,用户可以在其中绘制多边形区域.
我的问题是可以用结绘制多边形(见图)(我不知道结是否是正确的词).我没有找到阻止多边形得到结的简单方法.对于附加图像的情况,我希望移除小卷曲,甚至要平滑轮廓
你知道一种方法吗?
绘制多边形当用户触摸屏幕的过程中,确实使用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) 我有非常简单的View Controller来演示MKPolyline的这种奇怪的渲染行为.没有什么特别的,只是普通的api电话.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let p1 = CLLocationCoordinate2D(latitude: 51, longitude: 13)
var coords = [
p1,
CLLocationCoordinate2D(latitude: 51.1, longitude: 13),
CLLocationCoordinate2D(latitude: 51.2, longitude: 13),
CLLocationCoordinate2D(latitude: 51.3, longitude: 13)
]
let polyline = MKPolyline(coordinates: &coords, count: coords.count)
map.addOverlays([polyline], level: .aboveRoads)
let cam = MKMapCamera(lookingAtCenter: p1, fromDistance: 1000, pitch: 45, heading: 0)
map.setCamera(cam, animated: …
Run Code Online (Sandbox Code Playgroud) 我想根据速度等条件在折线视图上应用渐变.我能够MKPolyLineRenderer
使用自定义笔触和填充进行子类绘制,但只沿路径使用相同的颜色.
我看到了其他类似的问题:从圆圈或圆环 或OSX中绘制线段,如何渐变填充路径笔划?,但这不是我想要的.如果我理解正确,就不能用这些方法填充下面这样的路径.至少不作为一个整体对待.
一个非常相似的问题是Gradient Polyline和MapKit ios.但是它为点之间的每个段添加了叠加,我认为这不是太有效.
这是唯一可行的方法吗?(我需要绘制数百甚至数千个这些段...)如何轻松实现?
谢谢
mkpolyline ×10
ios ×9
mkmapview ×4
mapkit ×3
swift ×3
mkoverlay ×2
objective-c ×2
ios7 ×1
iphone ×1
mkpolygon ×1