Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检查阵列的经纬度是否相同_Ios_Swift_Swift3_Mkmapview_Mapkit - Fatal编程技术网

Ios 检查阵列的经纬度是否相同

Ios 检查阵列的经纬度是否相同,ios,swift,swift3,mkmapview,mapkit,Ios,Swift,Swift3,Mkmapview,Mapkit,我有一个类包含这些数据 class Events { var name: String! var latitude: Double! var longitude: Double! } 我用json中的数据来填充它 所以有些事件具有相同的lat和lon,但它们不是连续的,我的意思是,事件3和事件4并不相同,等等 所以我试着在地图上显示他们 填写此数组 var events = [Events]() 在这个for循环中,我正在制作引脚 for events in events

我有一个类包含这些数据

class Events {
   var name: String!
   var latitude: Double!
   var longitude: Double!
}
我用json中的数据来填充它

所以有些事件具有相同的lat和lon,但它们不是连续的,我的意思是,事件3和事件4并不相同,等等

所以我试着在地图上显示他们

填写此数组

var events = [Events]()
在这个for循环中,我正在制作引脚

 for events in events {
            let annotation = MKPointAnnotation()
            annotation.title = events.name

            annotation.coordinate = CLLocationCoordinate2D(latitude: events.latitude, longitude: events.longitude)
            mapView.addAnnotation(annotation)
        }
如何在部署管脚之前进行快速搜索,查看一个管脚与另一个管脚的lat和lon是否相同,添加一些数字以显示两者接近


非常感谢

使用
Set
查找唯一实例。为了使用
Set
基本元素,本例中的
事件必须是
可散列的
,并隐含
可相等的

class Events : Hashable {
    var name: String!
    var latitude: Double!
    var longitude: Double!

    // implement Hashable        
    var hashValue: Int {
        return latitude.hashValue | longitude.hashValue
    }

    // Implement Equatable
    static func ==(lhs:Events, rhs:Events) -> Bool {
        return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
    }
}
然后,主循环是现有循环的直接扩展,请注意,这会将所有匹配项压缩为一个点,并更改名称以指示有多少个匹配项:

// Use a Set to filter out duplicates
for event in Set<Events>(events) {
    let annotation = MKPointAnnotation()
    // Count number of occurrences of each item in the original array
    let count = events.filter { $0 == event }.count

    // Append (count) to the title if it's not 1
    annotation.title = count > 1 ? "\(event.name) (\(count))" : event.name

    // add to the map
}

使用
Set
查找唯一实例。为了使用
Set
基本元素,本例中的
事件必须是
可散列的
,并隐含
可相等的

class Events : Hashable {
    var name: String!
    var latitude: Double!
    var longitude: Double!

    // implement Hashable        
    var hashValue: Int {
        return latitude.hashValue | longitude.hashValue
    }

    // Implement Equatable
    static func ==(lhs:Events, rhs:Events) -> Bool {
        return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
    }
}
然后,主循环是现有循环的直接扩展,请注意,这会将所有匹配项压缩为一个点,并更改名称以指示有多少个匹配项:

// Use a Set to filter out duplicates
for event in Set<Events>(events) {
    let annotation = MKPointAnnotation()
    // Count number of occurrences of each item in the original array
    let count = events.filter { $0 == event }.count

    // Append (count) to the title if it's not 1
    annotation.title = count > 1 ? "\(event.name) (\(count))" : event.name

    // add to the map
}


别针的顺序重要吗?如果没有,Swift的本机
设置将是完美的。否则,您将需要使用第三方“有序集”implementation@Alexander不,没关系,你能给我写一个
Set
的例子吗?
Set
有很好的文档记录。请阅读语言指南上的内容,如果您在使用时遇到困难,请返回我们。也许这会有所帮助?lat/long值是否完全相同?如果不是,您可能需要一些公差值,比如两者的差值<.00001。对于具有重复lat/long值的事件对象,您想做什么?让第一个按数组顺序排列?稍微随机化这些值?(这就是你所说的“…添加一些数字以显示两者接近?”)管脚的顺序重要吗?如果没有,Swift的本机
设置将是完美的。否则,您将需要使用第三方“有序集”implementation@Alexander不,没关系,你能给我写一个
Set
的例子吗?
Set
有很好的文档记录。请阅读语言指南上的内容,如果您在使用时遇到困难,请返回我们。也许这会有所帮助?lat/long值是否完全相同?如果不是,您可能需要一些公差值,比如两者的差值<.00001。对于具有重复lat/long值的事件对象,您想做什么?让第一个按数组顺序排列?稍微随机化这些值?(这就是你所说的“…添加一些数字以显示两者接近?”)与Duncan的评论相关,如果预计lat/long不完全相同,您必须同时使用
hashValue
=
的实现,请记住
hashValue
必须为
=
返回true的任何对返回相同的值。事实上,问题是针对确切位置上的事件。现在,如果count>1,我可以改变lat和lon,对吗?如果你想改变后续匹配点的lat/long,这个过程会有很大的不同。但是
Hashable
的实现和使用
Set
检测重复项仍然有效。当我尝试向lat和lon添加一些额外值时,所有管脚都会到达该位置。你能帮我解决这个问题吗?哦,好的,我还设置了一个计数器标志,在第二次相同的时候,它将运行附加值。谢谢!与Duncan的评论相关,如果lat/long不希望完全相同,那么您必须同时使用
hashValue
=
的实现。请记住
hashValue
必须为
=
返回true的任何对返回相同的值。事实上,问题是在确切位置上的事件。现在,如果count>1,我可以改变lat和lon,对吗?如果你想改变后续匹配点的lat/long,这个过程会有很大的不同。但是
Hashable
的实现和使用
Set
检测重复项仍然有效。当我尝试向lat和lon添加一些额外值时,所有管脚都会到达该位置。你能帮我解决这个问题吗?哦,好的,我还设置了一个计数器标志,在第二次相同的时候,它将运行附加值。谢谢!