使用 GoogleMaps 时,myLocationEnabled 会在 swift 上更改为 isMyLocationEnabled

2024-02-23

我正在尝试使用谷歌地图 API 来显示用户当前位置。我已经为这个问题苦苦挣扎了好几个星期,代码可以运行,但当我更改模拟器中的位置时,代码不会更新到当前位置。

我的代码如下,在查看其他人如何执行此操作后,他们都有 mapView.myLocationEnabled = true 而对我来说,这会产生一个错误,指出“myLocationEnabled 已重命名为“isMylocationEnabled”。我看过一些最新的帖子(2 个月前),每个人都在使用 myLocationEnabled,似乎没有出现错误,为什么我会收到此消息?这可能是我当前位置未更新的原因吗?

import UIKit
import GoogleMaps

class FirstViewController: UIViewController, CLLocationManagerDelegate {


    @IBOutlet weak var mapView: GMSMapView!


    let locationManager = CLLocationManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        mapView.settings.myLocationButton = true
        mapView.settings.compassButton = true
    }
    override func viewDidAppear(_ animated: Bool) {
        locationManager.requestWhenInUseAuthorization()
    }
    func locationManager(manager: CLLocationManager , didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            locationManager.startUpdatingLocation()


            mapView.isMyLocationEnabled = true

            mapView.settings.myLocationButton = true
        }
    }
    func locationManager(manager: CLLocationManager ,didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {

            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            let marker = GMSMarker()
            marker.title = "Current Location"
            marker.map = mapView
            locationManager.stopUpdatingLocation()
        }
    }
}

所以我终于解决了这个问题!基本上,我使用 GoogleMaps 和 GooglePlaces 更新了现有的 Podfile,然后更新了 pod 以确保所有框架都能正常工作。然后我使用此代码来获取我当前的位置

    import UIKit

    import GoogleMaps


    class FirstViewController: UIViewController, CLLocationManagerDelegate {


@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()


override func viewDidLoad() {
    super.viewDidLoad()
    let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
    vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
    vwGMap.camera = camera

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    locationManager.distanceFilter = 500
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    self.view = vwGMap
    }

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        if (status == CLAuthorizationStatus.authorizedWhenInUse)

        {
            vwGMap.isMyLocationEnabled = true
        }
    }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let newLocation = locations.last
        vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
        vwGMap.settings.myLocationButton = true
        self.view = self.vwGMap
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
    marker.map = self.vwGMap
    }

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 GoogleMaps 时,myLocationEnabled 会在 swift 上更改为 isMyLocationEnabled 的相关文章

随机推荐

  • 使用 log4j 记录到数据库

    由于在 log4j 中javadoc http logging apache org log4j 1 2 apidocs org apache log4j jdbc JDBCAppender html is 警告 这个版本的 JDBCApp
  • 使用 Bottle 将列表从 python 传递到 js 的最佳方法是什么?

    我使用 Bottle 作为 Web 服务器 需要将 python 列表传递给 javascript 当我只执行 myList 时 Bottle 会对列表中的字符串转义单引号并将其显示为 039 反过来 JS 对它所得到的结果也不是很满意 我
  • 嵌入式二进制验证实用程序错误

    从昨天开始 Xcode 在尝试在我的 iPhone 上运行我的 WatchKit 应用程序时做了一些愚蠢的事情 它给了我错误 嵌入式二进制验证实用程序错误 错误 警告 是一个目录 它不是很有帮助 而且似乎在抱怨我的 Watchkit 扩展目
  • 如何在maven配置中正确指定jcenter存储库?

    在 Gradle 中 我只需添加 repositories jcenter 在 maven pom xml 中执行相同操作的最简单且正确的方法是什么 或者我在哪里可以获得 jcenter 存储库的正确 url 您必须像下面这样定义setti
  • (何时)m2crypto 会移植到 Python3 吗?

    m2crypto 是否正在向 Python3 移植 我将开始一个需要跨平台 跨语言加密的新玩具项目 m2crypto 看起来像是可行的方法 但我宁愿使用 Py3 以避免必须在任何地方显式转换为 UTF8 该库的移植是否正在进行中 代码位于h
  • 升级到 OS X Mavericks 后 Python 出现奇怪问题

    将我的 OS X Lion 升级到 Mavericks 后 我遇到了一些奇怪的问题 起初 它给了我分段故障 or 总线错误 经过一番搜索后 我发现它与readline图书馆 该解决方案描述为bugs python org curl O ht
  • alpine 包 py-pip 丢失

    我尝试使用 Docker compose 文件在我的 alpine 中安装 python pip 但出现以下错误 ERROR unsatisfiable constraints py pip missing required by worl
  • React 中的类变量与 ES6

    这个问题可能已经在其他地方得到了回答 但在标记为重复之前 请帮助我解决这个问题 我指的是使用react和d3的以下codepen https codepen io swizec pen oYNvpQ https codepen io swi
  • 滚动查看并刷新内容

    我使用以下用户界面 父级相对布局parentLayout尺寸为 800x600 宽 x 高 第二个相对布局childLayout 它是父布局的子布局 它的尺寸为 800x1000 即它大于父布局 parentLayout addView c
  • 在 xampp windows 中安装 Yaml

    我正在尝试安装YAML http www yaml org in XAMPP windows 64 bit 从以下位置下载了 dll 文件http pecl php net package yaml 1 1 1 windows http p
  • 如何使用子进程popen Python [重复]

    这个问题在这里已经有答案了 Since os popen正在被取代subprocess popen 我想知道如何转换 os popen swfdump tmp filename swf d to subprocess popen I tri
  • PHP ini file_get_contents 外部 url

    我使用以下 PHP 函数 file get contents http example com 每当我在某个服务器上执行此操作时 结果都是空的 当我在其他地方执行此操作时 结果就是页面的内容是什么 然而 当我在结果为空的服务器上 在本地使用
  • iOS 启动故事板:如何更改选项卡栏的色调颜色?

    我在做什么 使用 启动故事板 它非常简单 并且包含一个默认的 UITabBarController 我已在启动情节提要以及我的应用程序中将选项卡栏的 tintColor 设置为红色 我使用的是 Xcode 7 iOS 9 什么不起作用 启动
  • 为什么“IN”查询标签在sql存储过程中的成本如此之高?

    如何改善我的性能问题 我有一个带有 IN 的 sql 查询 我猜 IN 会造成一些代价高昂的性能问题 但我需要索引我的 sql 查询吗 Mysql查询 SELECT p ReferencedxxxId FROM Common xxxRefe
  • 插槽算作 Azure 中的 VM 吗?

    我目前有一个通过应用服务免费计划部署到 Azure 的 Web 应用程序 作为上线的一部分 我有兴趣转向使用插槽 这主要是因为它使我能够将新代码部署到暂存中 然后在经过验证后无缝切换 现在 要使用插槽 我知道我需要标准计划 并且该计划至少需
  • 如何通过 Gmail API 发送电子邮件?去

    我正在尝试通过以下方式发送电子邮件谷歌邮箱API https developers google com gmail api v1 reference users messages send using Go http golang org
  • python 导入应该花这么长时间吗?

    对于以下命令 time python test py 在此脚本上 test py import numpy as np from math import import matplotlib pyplot as plt import matp
  • 如何设置$_GET变量

    我如何设置变量 GET函数将能够使用 无需提交表单action GET GET包含在 URL 中传递给脚本的键 值 如果您有以下网址 http www example com test php a 10 b plop Then GET将包含
  • android c2dm编程示例

    我是 Android 应用程序开发新手 我正在使用 c2dm 开发 Android 应用程序 谁能告诉我如何从 c2dm 接收应用程序的注册 ID 请告诉我详细的例子和解释 public class C2dmEx extends Activ
  • 使用 GoogleMaps 时,myLocationEnabled 会在 swift 上更改为 isMyLocationEnabled

    我正在尝试使用谷歌地图 API 来显示用户当前位置 我已经为这个问题苦苦挣扎了好几个星期 代码可以运行 但当我更改模拟器中的位置时 代码不会更新到当前位置 我的代码如下 在查看其他人如何执行此操作后 他们都有 mapView myLocat