ios - 尝试在点击 Google map 标记时运行函数

标签 ios swift google-maps parse-platform

我正在尝试运行一个函数,该函数将在点击谷歌地图上的标记时播放视频,但是尽管没有错误,但我无法运行该函数。也许我的逻辑或控制流程有问题。下面是我的 View Controller 以及函数。

@IBOutlet weak var viewMap: GMSMapView!

var placesClient: GMSPlacesClient?

var url : NSURL?
var videoData : NSData?
var doUpload : Bool?
var FriendsOrPublic : String?
var dataPath : String?
var gmsPlace : GMSPlace?
var gpsCoordinates : CLLocationCoordinate2D?
let locationManager = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }
    //placesClient = GMSPlacesClient()
    var gmsPlace : GMSPlace?
    let placesClient = GMSPlacesClient.sharedClient()
    placesClient.currentPlaceWithCallback { (placeLikelihoods, error) -> Void in
        guard error == nil else {
            print("Current Place error: \(error!.localizedDescription)")
            return
        }

        if let placeLikelihoods = placeLikelihoods {
            for likelihood in placeLikelihoods.likelihoods {
                gmsPlace = likelihood.place
                //print("Current Place name \(gmsPlace.name) at likelihood \(likelihood.likelihood)")
                //print("Current Place address \(gmsPlace.formattedAddress)")
                //print("Current Place attributions \(gmsPlace.attributions)")
                //print("Current PlaceID \(gmsPlace.placeID)")
                self.gpsCoordinates = (gmsPlace!.coordinate)

            }
            //print(self.gpsCoordinates)

        }
    }
    let testObject = PFObject(className: "TestObject")
    testObject["foo"] = "bar"
    testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
        print("Object has been saved.")
    }

    print(url)
    print(videoData)
    print(doUpload)
    print(FriendsOrPublic)
    print(dataPath)
    if doUpload == true {
        Upload()
    }

    Download()
    viewMap.delegate = self

}

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    //print("locations = \(locValue.latitude) \(locValue.longitude)")
    gpsCoordinates = locValue
    let camera = GMSCameraPosition.cameraWithTarget(gpsCoordinates!, zoom: 16.9)
    //let camera = GMSCamera
    print(camera)
    viewMap.camera = camera
    viewMap.myLocationEnabled = true
    viewMap.settings.myLocationButton = false


    let marker = GMSMarker()
    marker.position = self.gpsCoordinates!
    marker.title = "Newport Beach"
    marker.snippet = "California"
    marker.map = viewMap
    locationManager.stopUpdatingLocation()
}
var marker : GMSMarker!
func Download() {
    let query = PFQuery(className:"UploadedVideoCurrent")

    //        query.whereKey("GPS", equalTo: "ChIJTbSOh8Pf3IARt311y2Wqspc")
    query.whereKey("typeofPost", equalTo: "Public")
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            print("Successfully retrieved \(objects!.count) coordinates.")
            // Do something with the found objects
            if let objects = objects {
                for object in objects {
                    print(object.objectForKey("GPS"))
                    let postsLat = object.objectForKey("GPS")!.latitude as Double
                    let postsLong = object.objectForKey("GPS")!.longitude as Double
                    let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: postsLat, longitude:postsLong)

                    self.marker = GMSMarker(position: position)
                    self.marker.map = self.viewMap
                }
            }
        } else {
            // Log details of the failure
            print("Error: \(error!) \(error!.userInfo)")
        }
    }
}
func viewMap(viewMap: GMSMapView, didTapMarker marker: GMSMarker)
{
    print("tapped")
}

最佳答案

您调用了错误的委托(delegate)函数。您仍然应该调用函数“mapView”,因为这就是委托(delegate)函数的定义方式,即使您的 map 本身被声明为“viewMap”

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
       print("tapped")
}

关于ios - 尝试在点击 Google map 标记时运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982279/

相关文章:

c# - 将 C# 函数转换为 Swift 4.2

swift - Autolayout - 带有 ImageView 的 UITableViewCell - 边距?

ios - Xcode 9 上的 GoogleMaps 崩溃

algorithm - Google Map的算法从2点返回路径是否是最短路径?

iphone - 如何从 youtube 下载视频并在 iphone sdk 中修剪或剪切下载的视频?

ios - 如何管理来自 QuickBlox 的推送通知的角标(Badge)计数器?

ios - 无法完全隐藏 UIView

ios - 使用 UIActivityViewController 检测启动了哪个 App

ios - 如何修复错误: Generic parameter 'T' could not be inferred

android - 为什么 Zoom 不能与 Google map 一起使用?