【问题标题】:drop pin with long touch in google maps在谷歌地图中长按放置图钉
【发布时间】:2016-06-11 06:47:25
【问题描述】:

我无法在谷歌地图中快速添加长触摸标记 我尝试了很多代码,但它对我不起作用 我该怎么办!?

override func viewDidLoad() {

    super.viewDidLoad()        
    GMSServices.provideAPIKey("AIzaSyBw95wEhcSiSBmPWuYkiK0_IBnZQK-Lm7I")


    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Error" + error.description)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let userLocation = locations.last
    let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

    let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude,
                                                      longitude: userLocation!.coordinate.longitude, zoom: 16)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    mapView.settings.myLocationButton = true
    view = mapView

   let  position = CLLocationCoordinate2DMake(10, 10)
    let marker = GMSMarker(position: position)


    marker.opacity = 0.6
    marker.position = center
    marker.title = "Current Location"
    marker.snippet = ""
    marker.map = mapView
    //mapView.clear()
}

蚂蚁帮助将不胜感激

【问题讨论】:

  • 您要拖动现有标记还是要在地图上添加新标记?
  • 新标记!目的地
  • 您想在长按上添加标记,而不是在地图视图上单击一次。
  • 是的!现在我该怎么办!?它的代码是什么?!
  • 试试我的答案,让我知道它是否有效。

标签: ios swift google-maps marker


【解决方案1】:

你试试这个。首先你需要像这样在mapView上添加长手势

对于目标 C

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.mapView addGestureRecognizer:self.longPress];

现在添加这个handleLongPress函数

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint longPressPoint = [recognizer locationInView:self.mapView];
        CLLocationCoordinate2D coordinate = [mapView.projection coordinateForPoint: point];
        //Now you have Coordinate of map add marker on that location
     }
}

快速

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector(("handleLongPress:")))
self.mapView.addGestureRecognizer(longPressRecognizer)

现在添加这个handleLongPress函数

@objc func handleLongPress(recognizer: UILongPressGestureRecognizer)
 {
    if (recognizer.state == UIGestureRecognizerState.began)
    {
        let longPressPoint = recognizer.location(in: self.mapView);
        let coordinate = mapView.projection.coordinate(for: longPressPoint )
        //Now you have Coordinate of map add marker on that location
        let marker = GMSMarker(position: coordinate)
        marker.opacity = 0.6
        marker.title = "Current Location"
        marker.snippet = ""
        marker.map = mapView
    }
}

希望对你有帮助

【讨论】:

  • 我得先把它改成swift!
  • 对不起,我没有注意到你想要快速的代码。让我快速编辑我的答案
  • 没有使用 Objective-C 选择器 'handleLongPress:' 声明的方法
  • 是运行时还是编译时?
  • 在这个页面查看我的答案!
【解决方案2】:

目前谷歌地图提供了一个长按覆盖功能,这没什么大不了的。这是一个示例代码:

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
    let marker = GMSMarker(position: coordinate)
   marker.icon = UIImage(named: "ic_pin_marker") // custom your own marker
   marker.map = mapView
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多