【问题标题】:Maps SDK Crashes iPhone (suspected memory leak?)Maps SDK 使 iPhone 崩溃(怀疑内存泄漏?)
【发布时间】:2021-01-12 00:09:45
【问题描述】:

我正在尝试在 xcode 中测试 Google Maps SDK 的基本功能。我在 Xcode 模拟器中运行了下面的代码,它运行得很好。但是,当我尝试在 iPhone 8 上运行它时,它会正确显示我的位置,但地图视图会不断重新加载,最终,我的手机内存已满,应用程序崩溃。我是使用 Google Maps SDK 的新手,尽管我怀疑这是某种内存泄漏。任何建议或帮助将不胜感激。

import CoreLocation
import GoogleMaps
import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {
        
    
    let manager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        manager.delegate = self
        
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        
        GMSServices.provideAPIKey("")
        // Do any additional setup after loading the view, typically from a nib.
        print("View has loaded")
        
        
        // Do any additional setup after loading the view.
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
     
        
        print("License: \n\n\(GMSServices.openSourceLicenseInfo())")
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        guard let location = locations.first else {
            return
        }
        
        let coordinate = location.coordinate
        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 12.5)
        let mapView = GMSMapView.map(withFrame: view.frame, camera: camera)
        view.addSubview(mapView)
        
        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = coordinate
        marker.title = "My Location"
        marker.snippet = "On Earth"
        marker.map = mapView
        
    }

}

..

【问题讨论】:

    标签: xcode memory memory-leaks crash maps


    【解决方案1】:

    您正在为每次位置更新添加另一个地图视图。这会很快消耗资源。

    相反,应该只添加一次地图视图,在位置更新开始之前,并且在位置更新例程中,只更新标记。

    【讨论】:

      猜你喜欢
      • 2011-12-28
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      相关资源
      最近更新 更多