【问题标题】:How to update MapView when Firebase sends location data?Firebase 发送位置数据时如何更新 MapView?
【发布时间】:2019-01-31 13:31:29
【问题描述】:

我正在尝试构建一个跟踪应用程序。我为每个用户进行了登录/注册。登录后,用户开始更新位置数据并将其发送到 Firebase,TableView 也会填充所有注册的用户。单击 TableView 中的用户后,我会显示一个带有 MapView 的新控制器。问题是我不知道如何在不单击按钮或类似按钮的情况下触发获取 Firebase 位置数据,并且数据不应该在退出 MapView 控制器之前停止出现,因为这个想法是实时显示用户的位置。

有什么建议吗?提前致谢!

【问题讨论】:

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

你可以在appdelegete类中调用location did update方法。

对于斯威夫特

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.last{
        let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
        self.map.setRegion(region, animated: true)
    }
}

对于目标 C

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{  
    if(!newLocation) return;

    if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {        
      // to  draw path as new location find
        jogPoint = [[JogPoint alloc] initWithCenterCoordinate:newLocation.coordinate];

        [jogPoints addObject:jogPoint];

     }
 }

【讨论】:

  • 该方法不是返回设备的位置吗?我需要使用从 Firebase 获得的位置数据(纬度和经度)更新 MapView
  • 这不是 OP 所要求的。他在问如何不断地从应用程序的其他用户那里获取位置数据 - 请参阅他的答案。
【解决方案2】:

好的,我找到了方法。我只需要使用 Firebases 构建观察者并设置它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2016-12-08
    • 2017-10-29
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多