Choose a location from Google maps by making a dynamic pin for annotations on iPhone

I want to create a dynamic pin on a Google map on an iPhone so that a user can drag a pin by clicking on it and put this pin on Google maps to indicate the location of their choice.
After the user puts a pin I want to get the location coordinates and the name of the location corresponding to this pin. Any suggestion on how to develop this on the iPhone.
thanks

+4
source share
2 answers

In your view controller, you must implement the methods from the MKMapViewDelegate and LongPressGestureAware protocol (which I created) to remove the output on the map when the user clicks and holds on the screen.

Your annotation must have the MKAnnotation and MKReverseGeocoderDelegate protocols implemented.

I am inserting code that may help you:

SimpleMapAnnotationViewController.h:

@interface SimpleMapAnnotationViewController : TTViewController<LongPressGestureAware, MKMapViewDelegate> { SimpleMapAnnotation *_dropPin; MKPinAnnotationView *_pinView; } 

SimpleMapAnnotationViewController.m:

 #pragma mark - #pragma mark LongPressGestureAware -(void) initLongPressGestureRecognizer { UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; [self.map addGestureRecognizer:longPressGesture]; [longPressGesture release]; } -(void)handleLongPressGesture:(UIGestureRecognizer*)sender { if([sender isMemberOfClass:[UILongPressGestureRecognizer class]] && (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateBegan)) { [self.map removeGestureRecognizer:sender]; //avoid multiple pins to appear when holding on the screen } CGPoint point = [sender locationInView:self.map]; CLLocationCoordinate2D theCoordinate = [self.map convertPoint:point toCoordinateFromView:self.map]; self.dropPin = [[[SimpleMapAnnotation alloc] initWithCoordinate:theCoordinate] autorelease]; [self.map addAnnotation:self.dropPin]; [self performSelector:@selector(selectInitialAnnotation) withObject:nil afterDelay:0.5]; } -(void)selectInitialAnnotation { [self.map selectAnnotation:[self.map.annotations objectAtIndex:0] animated:YES]; } #pragma mark - #pragma mark MKMapViewDelegate - (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation { if (annotation == self.map.userLocation){ return nil; //default to blue dot } MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"]; if (pin == nil) { pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"] autorelease]; } else { pin.annotation = annotation; } pin.canShowCallout = YES; pin.draggable = YES; pin.animatesDrop = YES; pin.pinColor = MKPinAnnotationColorGreen; self.pinView = pin; self.dropPin.pinView = self.pinView; return pin; } - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState { //NSArray *annotations = self.map.annotations; if (oldState == MKAnnotationViewDragStateDragging) { SimpleMapAnnotation *annotation = (SimpleMapAnnotation *)annotationView.annotation; [annotation updateSubtitle]; } if(newState == MKAnnotationViewDragStateEnding) { NSLog(@"drag finish"); } } 

SimpleMapAnnotation.h

 @interface SimpleMapAnnotation : NSObject <MKAnnotation, MKReverseGeocoderDelegate> { CLLocationCoordinate2D _coordinate; NSString *_title; NSString *_subtitle; MKPinAnnotationView *_pinView; } 

SimpleMapAnnotation.m

 - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate { self.coordinate = coordinate; MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:self.coordinate]; geoCoder.delegate = self; [geoCoder start]; self.subtitle = [NSString stringWithFormat:@"%f %f", self.coordinate.latitude, self.coordinate.longitude]; return self; } #pragma mark - #pragma mark MKReverseGeocoderDelegate // this delegate is called when the reverseGeocoder finds a placemark - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { MKPlacemark * myPlacemark = placemark; NSString *address = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStreetKey]; self.title = address; } // this delegate method is called if an error occurs in locating your current location - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); } // this delegate is called when the reversegeocoder fails to find a placemark - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { //invalid place } 
+3
source

You can read the documentation on map annotations. Here's a link

You can get location coordinates from anywhere in MapView using the convertPoint: toCoordinateFromView method: MapView method

+1
source

Source: https://habr.com/ru/post/1388502/


All Articles