【问题标题】:Marker not locating in google map in subview IOS SWIFT标记未在子视图 IOS SWIFT 的谷歌地图中定位
【发布时间】:2015-10-22 06:34:57
【问题描述】:

您好,我试图将地图放在子视图中,但是当我将谷歌地图放在子视图中时,标记不起作用,GPS 坐标也不起作用

-带子视图

-没有子视图

-SWIFT 代码

import UIKit
import GoogleMaps

class HomeViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: GMSMapView!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()

                let camera = GMSCameraPosition.cameraWithLatitude(15.4989, longitude: 73.8278, zoom: 6)
                let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
                mapView.myLocationEnabled = true
//                self.view = mapView
                self.view.addSubview(mapView)
                let marker = GMSMarker()
                marker.position = CLLocationCoordinate2DMake(15.4989, 73.8278)
                marker.title = "Panjim"
                marker.snippet = "Near Don Bosco,Alphran Plaza"
                marker.map?.addSubview(mapView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

提前致谢

【问题讨论】:

  • 您使用let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera),这将使mapView 的大小为零。此外,就像@mixth 建议的那样,您应该改用marker.map = mapView,将标记添加到您的mapView
  • 如果我喜欢它就会出现在视野之外

标签: ios swift google-maps


【解决方案1】:

我找到了解决方案。问题是:我正在创建一个新地图,然后在该新地图上添加一个标记。然后有了新地图,我什么也没做。 所以这是我的解决方案:

@IBOutlet weak var subviewMap: GMSMapView!

func loadMap() {
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
        subviewMap.camera = camera
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = subviewMap
    }

而且它有效。

注意:不要忘记在 IB 中将您的子视图提及为 GMSMapView 类

感谢@O-mkar 和@mixth 的努力。

快乐编码:]

【讨论】:

  • 谢谢,我也遇到了同样的问题!
【解决方案2】:

这里是添加标记的解决方案

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2DMake(lat, long)
            marker.appearAnimation = kGMSMarkerAnimationPop
            marker.title = "Marker" // Marker title here
            marker.snippet = "Tap the ↱ Navigate button to start navigating."
            marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0)
            marker.icon = UIImage(named: "marker") //Set marker icon here
            marker.map = self.mapView // Mapview here

动画相机定位

    let camera = GMSCameraPosition.camera(withLatitude: 15.4989, longitude: 73.8278, zoom: 17)
    mapView.animate(to: camera)

【讨论】:

    【解决方案3】:

    我的 GMSMapView 位于另一个 UIView 中,一切正常。唯一不同的是:

    marker.map = mapView
    

    【讨论】:

    • 我试过了,但它不适用于子视图...它只适用于主视图
    • @O-mkar 接受的答案不起作用,我面临同样的问题。你找到解决办法了吗?
    • @SML 我发布了一个新答案
    【解决方案4】:
    after adding marker you should add some delay with this approach i have added 2 marker with bounds 
    
    DispatchQueue.main.async {
                if self.markerArray.count > 1 {
                    var bounds = GMSCoordinateBounds()
                    for marker in self.markerArray {
                        marker.map = self.mapView
                        bounds = bounds.includingCoordinate(marker.position)
                    }
                    self.isMovedTheMap = false
                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.9, execute: {
                        self.superTopView.fadeOut()
                        let update = GMSCameraUpdate.fit(bounds, withPadding: 80)
                        self.mapView.animate(with: update)
                    })
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2011-07-16
      • 2014-07-18
      • 2019-02-08
      相关资源
      最近更新 更多