【问题标题】:Attempting to run a function when Google Maps Marker is tapped在点击 Google Maps Marker 时尝试运行功能
【发布时间】:2016-06-23 04:21:07
【问题描述】:

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

@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")
}

【问题讨论】:

  • 你在哪里播放视频,我在上面的 sn-p 中没有看到播放视频的代码。要播放视频,请编写一个播放视频的方法,并从 GMSMapView 的 didTapMarker 委托方法调用该方法,到目前为止,您只是在该方法中打印“tapped”。
  • @iamyogish 将来我打算播放视频,我的问题是该功能根本没有运行,因为打印语句从未运行。
  • 你能不能在 didUpdateLocations 方法的末尾打印出 viewMap 对象的 delegate 属性,然后检查 delegate 是否仍然是你的 ViewController?我怀疑这可能是委托未设置在适当位置或正在更改的情况。
  • @iamyogish 打印语句也永远不会在我的 didUpdateLocations 中运行
  • 安装应用程序时是否收到提示?请求您允许访问您的位置??

标签: ios swift google-maps parse-platform


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-10-25
    • 2015-07-25
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 2019-06-02
    • 2021-06-20
    相关资源
    最近更新 更多