在谷歌地图长按触针

时间:2016-06-11 06:47:26

标签: ios swift google-maps marker

我无法在swift中的Google地图中长时间添加标记 我尝试了很多代码,但它对我不起作用 我该怎么办??

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()
}

Ant的帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

你试试这个吗?首先,您需要在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: "handleLongPress:")
self.mapView.addGestureRecognizer(longPressRecognizer)

现在添加此handleLongPress函数

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

希望这会对你有所帮助

答案 1 :(得分:0)

通过Google地图提供长按功能,现在这并不重要。这是示例代码:

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
}