2017-04-22 1 views
0
내가 길게 누르면에 마커를 추가 할

에서 작동하지 않습니다하지만 내 didLongPressAt 좌표 이벤트가 작동하지 않습니다. 테스트를 위해 print 문을 사용했습니다. UIView가 추가되었습니다. 내장 제스처도 사용했지만 여전히 작동하지 않습니다. 내 코드는 다음과 같습니다. 제발 도와주세요.기본 제스처 이벤트는 GoogleMap으로

var mapView=GMSMapView() 

var camera = GMSCameraPosition() 
let locationmanager = CLLocationManager() 



override func viewDidLoad() { 
    super.viewDidLoad() 

    self.mapView.delegate=self 


    // Do any additional setup after loading the view. 
    self.locationmanager.delegate=self 
    self.locationmanager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters 
    self.locationmanager.requestWhenInUseAuthorization() 
    self.locationmanager.startUpdatingLocation() 

} 


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    self.showCurrentLocationOnMap() 
    self.locationmanager.stopUpdatingLocation() 

} 

func showCurrentLocationOnMap() 
{ 
    camera = GMSCameraPosition.camera(withLatitude: (self.locationmanager.location?.coordinate.latitude)!, longitude: (self.locationmanager.location?.coordinate.longitude)!, zoom: 15) 
    mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera) 



    mapView.settings.myLocationButton=true 
    mapView.isMyLocationEnabled=true 

    let marker = GMSMarker() 
    marker.position=camera.target 
    marker.snippet="My Current Location" 
    marker.appearAnimation=GMSMarkerAnimation.pop 
    marker.map=mapView 
    myView.addSubview(mapView) 

} 


func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) { 
    print (coordinate.latitude) 
    print(coordinate.longitude) 
} 

답변

0

문제는 여기에 당신이 자기로 설정되지 않은 여기에 만들 mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera) 새로운 GMSMapView의 인스턴스의 대리자를 수행하여 다시지도보기를 초기화하는 것을입니다. 따라서 대리자 메서드 (didLongPressAt)가 호출되지 않습니다. 그냥 카메라의 위치를 ​​변경하려는 경우

대신, 현재 위치 기회는 대신 mapView.animate(with cameraUpdate: GMSCameraUpdate)를 사용하는 경우.

override func viewDidLoad() { 
    super.viewDidLoad() 
    let camera = GMSCameraPosition.camera(withLatitude: (self.locationmanager.location?.coordinate.latitude)!, longitude: (self.locationmanager.location?.coordinate.longitude)!, zoom: 15) 

    mapView = GMSMapView.map(withFrame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(myView.frame.size.width), height: CGFloat(myView.frame.size.height)), camera: camera) 
    self.mapView.delegate=self 


    // Do any additional setup after loading the view. 
    self.locationmanager.delegate=self 
    self.locationmanager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters 
    self.locationmanager.requestWhenInUseAuthorization() 
    self.locationmanager.startUpdatingLocation() 

} 

func showCurrentLocationOnMap() 
{ 
    let newLocation = CLLocationCoordinate2D(latitude: self.locationmanager.location?.coordinate.latitude, self.locationmanager.location?.coordinate.longitude: longitude) 
    let cameraPositionUpdate = GMSCameraUpdate.setTarget(newLocation) 
    self.mapView.animate(with: cameraPositionUpdate) 

    // Code to add the marker 
} 
+0

mapView.animate (with cameraUpdate : GMSCameraUpdate) 작동하지 않습니다. 애니메이션을 호출 할 수 없다는 오류가 표시됩니다. –

+0

GMSCameraUpdate의 인스턴스를 만든 다음이 메서드에 전달해야합니다. 위에서 언급 한 코드는 단지 프로토 타입 일뿐입니다. – dRAGONAIR

+0

당신은 "showCurrentLocationOnMap" 에서 newLocation = CLLocationCoordinate2D (latitude : self.locationmanager.location? .coordinate.latitude, self.locationmanager.location?. coordinate.longitude : longitude)와 같은 것을 수행해야합니다. let cameraPositionUpdate = GMSCameraUpdate.setTarget (newLocation) self.mapView.animate (with : cameraPositionUpdate)' – dRAGONAIR