i am newbie in iOS Development and new in map kit i want to know about Latitude and Longitude of MapView Center Every time Like as When User Scrollfrom one location to Another then i want to Show Center Latitude and Longitude of my GMSMapView how it Possible like as Here i Attach Image for it.
Here my Current Location is Mumbai Then It Shows My mapView like as
and i want to know Center Location Latitude and Longitude like as when user Scroll Mapview then i want to Know Latitude and Longitude of My Image Indicator Point. Here image Show as i want to Know Latitude and Longitude of this Point Here My Image indicated is in Center Position of my MapView.
To get the position whenever the map is moved, you'd want to handle mapView:didChangeCameraPosition:
.
Then inside that method, you can access mapView.camera.target
to get the current centre of the map.
- (void) mapView: (GMSMapView *)mapView
didChangeCameraPosition: (GMSCameraPosition *)position
{
double latitude = mapView.camera.target.latitude;
double longitude = mapView.camera.target.longitude;
// now do something with latitude and longitude
}
Note that to handle the delegate method, you'll need to implement the GMSMapViewDelegate
protocol. When setting up the map, you'd need to set the map's delegate to the class which handles the protocol (usually self
), eg:
mapView.delegate = self;
Use below code for getting center of mapview latitude and longitude . When ever you scroll mapview in any direction automatically regionWillChangeAnimated: method will call.It's one of the optinal delegate method.
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(mkmapView.centerCoordinate.latitude, mkmapView.centerCoordinate.longitude);
CGFloat txtLatitude = coord.latitude;
CGFloat txtLongitude = coord.longitude;
NSLog(@"Latitude is===>>>%f %f",txtLongitude);
NSLog(@"Longitude is===>>>%f %f",txtLongitude);
}
If you tap the map view,you need that Latitude and Longitude use below code. Here _mkMapView is outlet of Mapview.
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
[self.view addGestureRecognizer:tapGesture];
}
- (void)backgroundTapped:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint = [gesture locationInView:_mkMapView];
CLLocationCoordinate2D location =[_mkMapView convertPoint:touchPoint toCoordinateFromView:_mkMapView];
}
may be it will helpful for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With