【问题标题】:Refresh markers without refreshing entire Google map (Swift 2.0, Xcode v 7.0.1)刷新标记而不刷新整个 Google 地图(Swift 2.0,Xcode v 7.0.1)
【发布时间】:2016-01-29 01:45:53
【问题描述】:

我正在使用 Swift 2.0 中的 Google Maps SDK 构建一个在地图上显示公交车位置的应用。我有一个 XML 文件,该文件每 10 秒更新一次,并显示所有活动穿梭机的经纬度。在我的代码中,我有标记对象,其中包含纬度、经度、ID 和公共汽车名称的字段。

我已将显示地图的代码放置在我想要显示总线的位置的中心,以及迭代所有总线位置的 for 循环以在地图上显示它们作为名为 realoadBuses 的函数中的标记()。在这个函数中,我还有与 XML 文件通信的代码行。

我使用 NSTimer() 每 10 秒调用一次 realoadBuses(),以便相应地更新公交车的位置。

我的代码如下所示:

// runs reloadBuses() every 10 seconds
    timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "reloadBuses", userInfo: nil, repeats: true)

func reloadBuses() {

    // displays the map adjusted on Campus
    let camera = GMSCameraPosition.cameraWithLatitude(37.0000,
        longitude: -122.0600, zoom: 14)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.mapType = kGMSTypeNormal
    mapView.myLocationEnabled = true
    self.view = mapView

    // XML file
    parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://link.to.xml.file.xml"))!)!

    let coord = Coord2()
    parser.delegate = coord
    parser.parse()
    print("coord has a count attribute of \(coord.count)")
    print("coord has \(coord.markers.count) markers")

    // loops through all the lats and lngs of the buses and produces a marker
    // for them on our Google Maps app
    for marker in coord.markers {
        print("marker id=\(marker.id), lat=\(marker.lati), lng=\(marker.lngi), route=\(marker.route)")

        // displays the buses
        let buses = GMSMarker()
        buses.position = CLLocationCoordinate2DMake(marker.lati, marker.lngi)
        buses.title = marker.route
        if buses.title == "UPPER CAMPUS" {
            buses.icon = UIImage(named: "uppercampus")
        } else if buses.title == "LOOP" {
            buses.icon = UIImage(named: "innerloop")
        }
        buses.snippet = marker.id
        buses.map = mapView
    }
}

我遇到的问题是,每次 NSTimer() 调用 reloadBuses() 时,地图都会与公交车标记一起刷新。

我知道会发生这种情况,因为我有在 reloadBuses() 函数中显示地图的代码。我试图在调用 NSTimer() 之前将该代码块放在某处,但是 ma​​pView 超出了 reloadBuses() 内 for 循环中最后一行所需的范围。

然后我尝试通过这样做将 mapView 传递给 reloadBuses()

// displays the map adjusted on Campus
let camera = GMSCameraPosition.cameraWithLatitude(37.0000,
longitude: -122.0600, zoom: 14)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.mapType = kGMSTypeNormal
mapView.myLocationEnabled = true
self.view = mapView

// runs reloadBuses() every 10 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "reloadBuses(mapView)", userInfo: nil, repeats: true)

reloadBuses(map: GMSMapView){
    ...
    for loop {
        ...
        buses.map = map
}

代码将成功构建,地图将显示没有任何标记(巴士),但大约几秒钟后应用程序会崩溃,我会收到此错误:

**2015-10-28 19:42:05.731 GeoBus[1926:107070]-[GeoBus.ViewController reloadBuses(self.view)]:无法识别的选择器发送到实例 0x7f91784aebc0 2015-10-28 19:42:05.743 GeoBus [1926:107070] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[GeoBus.ViewController reloadBuses(mapView)]:无法识别的选择器发送到实例 0x7f91784aebc0” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010600bf65 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000107cd9deb objc_exception_throw + 48 2核心基础0x000000010601458d-[NSObject(NSObject)不识别选择器:]+205 3 核心基础 0x0000000105f61f7a ___forwarding___ + 970 4 核心基础 0x0000000105f61b28 _CF_forwarding_prep_0 + 120 5 基础 0x00000001063f1671 __NSFireTimer + 83 6 核心基础 0x0000000105f6c364 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20 7 核心基础 0x0000000105f6bf11 __CFRunLoopDoTimer + 1089 8 核心基础 0x0000000105f2d8b1 __CFRunLoopRun + 1937 9 核心基础 0x0000000105f2ce98 CFRunLoopRunSpecific + 488 10 图形服务 0x0000000109e03ad2 GSEventRunModal + 161 11 UIKit 0x0000000106828676 UIApplicationMain + 171 12 地理总线 0x0000000103ca7bfd 主要 + 109 13 libdyld.dylib 0x000000010880292d 开始 + 1 14 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止**

【问题讨论】:

    标签: ios swift google-maps-markers google-maps-sdk-ios unrecognized-selector


    【解决方案1】:

    是的,每次调用都会重新创建地图:

    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.mapType = kGMSTypeNormal
    mapView.myLocationEnabled = true
    self.view = mapView
    

    我建议您改为在界面构建器中设置地图视图,并通过 IB 向您的类添加出口的能力来获得与它的绑定。

    至于移动标记,您需要做的是在代码中保持数据结构中的标记并移动它们,而不是尝试在每次从服务器刷新数据时重新创建显示。

    【讨论】:

    • 如何在不从每 10 秒生成的 XML 文件中检索新的纬度和经度的情况下移动公共汽车?我必须每 10 秒解析一次 XML 文件才能获得新位置。
    • 谢谢布雷特。在界面生成器中设置 mapView 有效!请注意,我不必设置新的数据结构。我将标记保留为对象。
    • @Brett 。我正面临谷歌地图图标闪烁的问题,你能指导我为什么会发生这种情况
    • @UmaMadhavi 我建议您使用足够的信息开始一个新问题,以便人们能够重现您的问题。谢谢!
    猜你喜欢
    • 2017-10-08
    • 2021-01-14
    • 2016-02-17
    • 2014-03-23
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多