ios - 形状,多边形,不会 swift 显示在 GoogleMaps 上

标签 ios swift google-maps polygon

我无法在 map 上显示我的多边形。下面是我的代码。

import UIKit

import MapKit

import CoreLocation

import GoogleMaps

import GooglePlaces

class ViewController: UIViewController{


    @IBOutlet weak var mapView: GMSMapView!



        //private let locationManager = CLLocationManager()

        var placesClient: GMSPlacesClient!

        var locationManager = CLLocationManager()
        var mapsView : GMSMapView!
        var zoomLevel: Float = 15.0
        var polygon = GMSPolygon()
        var didFindMyLocation = false
       // let overlay = UIView()


        let defaultLocation = CLLocation(latitude: -33.869405, longitude: 151.99)
    let path = GMSMutablePath()



        override func viewDidLoad() {

            super.viewDidLoad()

            mapsView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            mapsView.isMyLocationEnabled = true

            view.addSubview(mapsView)

            mapsView.isHidden = true


        }

    func createpolygon() {
        // Create a rectangular path
        let rect = GMSMutablePath()
        rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
        rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
        rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
        rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))

        // Create a polygon and assign it to the map.
        let polygon = GMSPolygon(path: rect)
        polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
        polygon.strokeColor = .black
        polygon.strokeWidth = 2
        polygon.map = mapsView
    }

我不知道是否需要添加叠加层。

最佳答案

你有两个 mapView 只使用其中一个

@IBOutlet weak var mapView: GMSMapView!
var mapsView : GMSMapView!

像这样

class ViewController: UIViewController{

@IBOutlet weak var mapView: GMSMapView!


    //private let locationManager = CLLocationManager()

    var placesClient: GMSPlacesClient!

    var locationManager = CLLocationManager()
    var zoomLevel: Float = 15.0
    var polygon = GMSPolygon()
    var didFindMyLocation = false
   // let overlay = UIView()


    let defaultLocation = CLLocation(latitude: -33.869405, longitude: 151.99)
let path = GMSMutablePath()



    override func viewDidLoad() {

        super.viewDidLoad()

        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        mapView.isMyLocationEnabled = true

        mapView.isHidden = true


    }

func createpolygon() {
    // Create a rectangular path
    let rect = GMSMutablePath()
    rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
    rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
    rect.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
    rect.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))

    // Create a polygon and assign it to the map.
    let polygon = GMSPolygon(path: rect)
    polygon.fillColor = UIColor(red: 0.25, green: 0, blue: 0, alpha: 0.05);
    polygon.strokeColor = .black
    polygon.strokeWidth = 2
    polygon.map = mapView
}

关于ios - 形状,多边形,不会 swift 显示在 GoogleMaps 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048755/

相关文章:

ios - 谁能用 MKMapView::mapRectThatFits 解释这些不准确之处

swift - 我怎样才能只允许从门户进入我的虚拟场景?

swift - 在 UICollectionViewCell 中设置 firebase 值时出现 Nil 可选解包错误

javascript - 单击侧边栏元素上的 Google Cluster map 打开特定信息窗口

javascript - 如何删除 Javascript 数组中的方括号?

ios - 如何测试支持 IPv6 的 iOS 应用程序? Apple 拒绝了应用程序,因为它不兼容 IPv6

ios - 对 xxx 的引用不明确

iphone - 如何在 xcode 的文件模板中创建文件夹?

ios - 设置 TextView 以查看数组中的所有元素

google-maps - 如何删除默认标记?