Ios 如何计算我的当前位置与MapView中的其他接点之间的距离
我是Swift新手,我需要计算当前位置附近最近的位置。您能告诉我应该使用哪个函数来计算我所在位置与我周围最近位置之间的距离吗。我必须在应用程序中显示距离和位置,以便用户可以选择最适合他的位置。我想我应该使用纬度和经度坐标,可以与我的坐标进行比较。我还发现我必须使用distanceFromLocation,但我不知道如何使用,如果有人给我提供一个示例,我会很高兴,我可以将其用于我的代码。 到目前为止,我的代码是:Ios 如何计算我的当前位置与MapView中的其他接点之间的距离,ios,mkmapview,distance,Ios,Mkmapview,Distance,我是Swift新手,我需要计算当前位置附近最近的位置。您能告诉我应该使用哪个函数来计算我所在位置与我周围最近位置之间的距离吗。我必须在应用程序中显示距离和位置,以便用户可以选择最适合他的位置。我想我应该使用纬度和经度坐标,可以与我的坐标进行比较。我还发现我必须使用distanceFromLocation,但我不知道如何使用,如果有人给我提供一个示例,我会很高兴,我可以将其用于我的代码。 到目前为止,我的代码是: class ViewThree: UIViewController, CLLoca
class ViewThree: UIViewController, CLLocationManagerDelegate{
@IBOutlet weak var SegmentControl: UISegmentedControl!
@IBOutlet weak var Mapview: MKMapView!
var manager = CLLocationManager()
var receiveImeNaSladkarnica: String = ""
var KordaA: String = ""
var KordaB: String = ""
var PodImeNaObekt: String = ""
override func viewDidLoad() {
super.viewDidLoad()
let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake((KordaA as NSString).doubleValue,(KordaB as NSString).doubleValue)
let objectAnn = MKPointAnnotation()
objectAnn.coordinate = pinLocation
objectAnn.title = receiveImeNaSladkarnica
objectAnn.subtitle = PodImeNaObekt
self.Mapview.addAnnotation(objectAnn)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func Directions(sender: AnyObject) {
UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/maps?daddr=\((KordaA as NSString).doubleValue),\((KordaB as NSString).doubleValue))")!)
}
@IBAction func MapType(sender: AnyObject) {
if (SegmentControl.selectedSegmentIndex == 0){
Mapview.mapType = MKMapType.Standard
}
if (SegmentControl.selectedSegmentIndex == 1){
Mapview.mapType = MKMapType.Satellite
}
}
@IBAction func LocateMe(sender: AnyObject) {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
Mapview.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation: CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true )
}
您可以使用
distance fromLocation
方法获取距离
let distance = userlocation.distanceFromLocation(YourPinInMap)
您可以使用
distance fromLocation
方法获取距离
let distance = userlocation.distanceFromLocation(YourPinInMap)
我在另一个应用程序中也遇到了同样的情况 在CLLocation对象中,有一个实例函数: 函数距离fromLocation(位置:CLLocation)->CLLocationDistance 已更新
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locationStrings = ["42.6977,23.3219","43.6977,24.3219"]
// This array must be an array that contains CLLocation objects
var locations: [CLLocation] = []
// We must retrieve the latitude and longitude from locationStrings array to convert them into CLLocation objects
for locationString in locationStrings {
let location = CLLocation(latitude: <latitude_value>, longitude: <latitude_value>)
locations.append(location)
}
// Then you will be able to enumerate through the array
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
对于您的用例
let locations = ... // All locations you want to compare
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
我的代码:
改进
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locationStrings = ["42.6977,23.3219","43.6977,24.3219"]
// This array must be an array that contains CLLocation objects
var locations: [CLLocation] = []
// We must retrieve the latitude and longitude from locationStrings array to convert them into CLLocation objects
for locationString in locationStrings {
let location = CLLocation(latitude: <latitude_value>, longitude: <latitude_value>)
locations.append(location)
}
// Then you will be able to enumerate through the array
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
func locationManager(管理器:CLLocationManager,didUpdateLocations位置:[CLLocation]){
将userlocation:CLLocation=位置[0]设为CLLocation
manager.stopUpdateLocation()
让location=CLLocationCoordinate2D(纬度:userlocation.coordinate.lation,经度:userlocation.coordinate.longitude)
设span=MKCoordinateSpanMake(0.5,0.5)
let region=MKCoordinateRegion(中心:位置,跨度:跨度)
Mapview.setRegion(区域,动画:true)
let locationStrings=[“42.6977,23.3219”,“43.6977,24.3219”]
//此数组必须是包含CLLocation对象的数组
变量位置:[CLLocation]=[]
//我们必须从LocationString数组中检索纬度和经度,以将它们转换为CLLocation对象
对于locationString中的locationString{
let location=CLLocation(纬度:,经度:)
位置。追加(位置)
}
//然后您将能够枚举整个数组
对于位置中的位置{
让distanceMeters=userLocation.distanceFromLocation(位置)
如果距离计>5000{//一些距离过滤器
//不显示此位置
}否则{
//显示此位置
}
}
我在另一个应用程序中也遇到了同样的情况
在CLLocation对象中,有一个实例函数:
函数距离fromLocation(位置:CLLocation)->CLLocationDistance
已更新
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locationStrings = ["42.6977,23.3219","43.6977,24.3219"]
// This array must be an array that contains CLLocation objects
var locations: [CLLocation] = []
// We must retrieve the latitude and longitude from locationStrings array to convert them into CLLocation objects
for locationString in locationStrings {
let location = CLLocation(latitude: <latitude_value>, longitude: <latitude_value>)
locations.append(location)
}
// Then you will be able to enumerate through the array
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
对于您的用例
let locations = ... // All locations you want to compare
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
我的代码:
改进
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userlocation:CLLocation = locations[0] as CLLocation
manager.stopUpdatingLocation()
let location = CLLocationCoordinate2D(latitude: userlocation.coordinate.latitude, longitude: userlocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.5, 0.5)
let region = MKCoordinateRegion(center: location, span: span)
Mapview.setRegion(region, animated: true)
let locationStrings = ["42.6977,23.3219","43.6977,24.3219"]
// This array must be an array that contains CLLocation objects
var locations: [CLLocation] = []
// We must retrieve the latitude and longitude from locationStrings array to convert them into CLLocation objects
for locationString in locationStrings {
let location = CLLocation(latitude: <latitude_value>, longitude: <latitude_value>)
locations.append(location)
}
// Then you will be able to enumerate through the array
for location in locations {
let distanceMeters = userLocation.distanceFromLocation(location)
if distanceMeters > 5000 { // Some distance filter
// Don't display this location
} else {
// Display this location
}
}
func locationManager(管理器:CLLocationManager,didUpdateLocations位置:[CLLocation]){
将userlocation:CLLocation=位置[0]设为CLLocation
manager.stopUpdateLocation()
让location=CLLocationCoordinate2D(纬度:userlocation.coordinate.lation,经度:userlocation.coordinate.longitude)
设span=MKCoordinateSpanMake(0.5,0.5)
let region=MKCoordinateRegion(中心:位置,跨度:跨度)
Mapview.setRegion(区域,动画:true)
let locationStrings=[“42.6977,23.3219”,“43.6977,24.3219”]
//此数组必须是包含CLLocation对象的数组
变量位置:[CLLocation]=[]
//我们必须从LocationString数组中检索纬度和经度,以将它们转换为CLLocation对象
对于locationString中的locationString{
let location=CLLocation(纬度:,经度:)
位置。追加(位置)
}
//然后您将能够枚举整个数组
对于位置中的位置{
让distanceMeters=userLocation.distanceFromLocation(位置)
如果距离计>5000{//一些距离过滤器
//不显示此位置
}否则{
//显示此位置
}
}
当locA和locB为CLLocation类型时,通过那边的lat long
当locA和locB处于CLA位置时,位置类型通过那边的lat long非常感谢。您知道如何比较多个位置,因为在我们的例子中,现在我只能比较我的位置和位置B,但我还想检查位置C、D、E等…并显示距离我5公里半径内的所有位置(仅示例)。没有问题!对于该用例,您需要将要比较的位置保留在数组中,然后使用For循环迭代该数组,并将每个位置与用户位置进行比较。使用if语句检查计算的距离是否大于要筛选的距离。检查更新的回答您是否查看它我会得到错误另外,如何在数组中添加坐标:[“42.6977,23.3219”,“43.6977,24.3219”]或者?非常感谢,它工作得非常完美。你知道我如何比较多个位置吗,因为在我们的例子中,现在我只能比较我的位置和位置B,但我还想检查位置C、D、E等…并显示距离我5公里半径内的所有位置(仅示例)。没有问题!对于该用例,您需要将要比较的位置保留在数组中,然后使用For循环迭代该数组,并将每个位置与用户位置进行比较。使用if语句检查计算的距离是否大于要筛选的距离。检查更新的回答您是否查看它我会得到错误另外,如何在数组中添加坐标:[“42.6977,23.3219”,“43.6977,24.3219”]或者?非常感谢,它工作得非常完美。你知道我如何比较多个位置吗,因为在我们的例子中,现在我只能比较我的位置和位置B,但我还想检查位置C、D、E等…并显示距离我5公里半径内的所有位置(仅示例)。非常感谢,它工作得非常完美。你知道我如何合作吗