gpt4 book ai didi

swift - 如何将图像叠加层添加到 MKMapView?

转载 作者:行者123 更新时间:2023-11-30 10:43:07 26 4
gpt4 key购买 nike

----------已更新------------
原问题在底部

<小时/>

我已经走了很远了,现在我有了这个:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet var mapView: MKMapView!

var locationManager: CLLocationManager!
var mapOverlay: MKOverlay!

override func viewDidLoad() {
super.viewDidLoad()

var points = [CLLocationCoordinate2D(latitude: -29.8122, longitude: 148.6351),
CLLocationCoordinate2D(latitude: -27.9307, longitude: 148.6351),
CLLocationCoordinate2D(latitude: -27.9307, longitude: 150.9909),
CLLocationCoordinate2D(latitude: -29.8122, longitude: 150.9909)]
let tile = MKPolygon(coordinates: &points, count: points.count)
tile.title = "zurich"
mapView.addOverlay(tile)

//Setup our Location Manager
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()

//Setup our Map View
mapView.delegate = self
mapView.mapType = MKMapType.satellite
mapView.showsUserLocation = true
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// mapView delegate function
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay: overlay)
renderer.fillColor = UIColor.red
return renderer
}
}

我现在需要知道如何将 renderer.fillColor = UIColor.red 替换为可显示我的图像的内容。再次感谢
----- 原始问题 ------

所以,我是 Swift 和 MapKit 的新手,我想在 MKMapView 之上添加一个简单的图像叠加层。我找到了一些答案,但它们都很令人困惑,而且它们都适用于 Swift 3 及更早版本。

我发现需要 map View 的委托(delegate),这是一个文件吗?我已经使用主视图 Controller 创建了一个 map View 。

这是我到目前为止所做的(在 ViewController.swift 文件中):

import UIKit
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

}

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib

let location = CLLocationCoordinate2D(latitude: 47.457925,
longitude: 8.548466)

let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)


}


}

谢谢您,希望您能帮忙!

最佳答案

有很多方法可以将图像嵌入到 map 中。

  1. 注释 View
  2. 标注
  3. 自定义 map 图 block

详细说明您的需求,也许我们可以更好地帮助您实现这一目标。

<小时/>

您正在 map 上添加叠加层。我们想要更改特定的 map 图 block 。

func createLocalUrl(forImageNamed name: String) -> URL? {

let fileManager = FileManager.default
let cacheDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let url = cacheDirectory.appendingPathComponent("\(name).png")

guard fileManager.fileExists(atPath: url.path) else {
guard
let image = UIImage(named: name),
let data = image.pngData()
else { return nil }

fileManager.createFile(atPath: url.path, contents: data, attributes: nil)
return url
}

return url
}

func setupTiles() {
let url = createLocalUrl(forImageNamed: "yourImageName")
let template = url?.absoluteString
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
self.tileOverlay = overlay
mapView.addOverlay(overlay)
self.tileRenderer = MKTileOverlayRenderer(tileOverlay: overlay)
}

func isInDesiredArea(middlePoint: MKMapPoint) -> Bool {

//mapView has convert function which converts CGPoint ->
//CLLocationCoordinate2D and vice versa Use this function and,
//Your polygon has boundingMapRect which has contains function.
//Also your map has func mapView(_ mapView: MKMapView,
//regionDidChangeAnimated animated: Bool) which runs whenever region changes..
return myBoundsPolygon.boundingMapRect.hasContain(middlePoint)

}


func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
//Convert middle point of your view to CLLocationCoordinate2D
//Convert your coordinate to MKMapPoint
if isInDesiredArea(middlePoint: point) {
setupTiles()
}
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
....
if overlay is MKTileOverlay {
return tileRenderer
}

关于swift - 如何将图像叠加层添加到 MKMapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com