gpt4 book ai didi

ios - 如何以编程方式快速打开带有坐标的 map 应用程序?

转载 作者:IT王子 更新时间:2023-10-29 04:56:40 26 4
gpt4 key购买 nike

我有要在我的 map 应用程序中打开的纬度和经度。我尝试了来自 HERE 的这段代码.

    func goToMap(){

var lat1 : NSString = self.venueLat
var lng1 : NSString = self.venueLng

var latitude:CLLocationDegrees = lat1.doubleValue
var longitude:CLLocationDegrees = lng1.doubleValue

var coordinate = CLLocationCoordinate2DMake(latitude, longitude)

var placemark : MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil)

var mapItem:MKMapItem = MKMapItem(placemark: placemark)

mapItem.name = "Target location"

let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey)

var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation()

MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions)

}

此函数成功打开 map ,但未显示任何图钉。它还显示了我不想要的用户位置。我只想在 map 上根据提供的纬度和经度定位。

最佳答案

这段代码对我来说工作正常。

func openMapForPlace() {

let lat1 : NSString = self.venueLat
let lng1 : NSString = self.venueLng

let latitude:CLLocationDegrees = lat1.doubleValue
let longitude:CLLocationDegrees = lng1.doubleValue

let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(MKCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(MKCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "\(self.venueName)"
mapItem.openInMapsWithLaunchOptions(options)

}

对于 swift 3.0:

import UIKit
import MapKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
openMapForPlace()
}

func openMapForPlace() {

let latitude: CLLocationDegrees = 37.2
let longitude: CLLocationDegrees = 22.9

let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Place Name"
mapItem.openInMaps(launchOptions: options)
}
}

swift 5:

let latitude: CLLocationDegrees = Double(K.latitude)!
let longitude: CLLocationDegrees = Double(K.longitude)!
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = K.companyName
mapItem.openInMaps(launchOptions: options)

关于ios - 如何以编程方式快速打开带有坐标的 map 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28604429/

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