2016-06-11 5 views
-2

Я не могу добавить маркер с длинным касанием на Google Maps в swift Я попробовал много кодов, но он не работает для меня что мне делать??drop pin с длинным касанием в google maps

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 помощь будет признателен

+0

вы хотите перетащить существующий маркер или хотят добавить новый маркер на карте? –

+0

новый маркер! для адресата –

+0

Вы хотите добавить маркер при длинном нажатии, не на одном касании на карте. –

ответ

1

Вы попробовать это. Прежде всего, необходимо добавить длинный жест MAPview как этот

Для Objective 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 
    } 
} 

Надеется, что это поможет вам

+0

Мне нужно сначала сменить его! –

+0

Sry, я не знал, что вы хотите, чтобы код был быстрым. Позвольте мне отредактировать мой ответ быстрым –

+0

Thanx много ......! –