MKMapView centerLocation - ios

In my application there is an MKMapView and I am trying to get the center coordinates of the map region that is currently visible. I am using following method so that if user moves the visible region I'll get new center coordinates.
- (void)mapView:(MKMapView *)mapView1 regionDidChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D centre = [mapView centerCoordinate];
NSLog(#"MAP CENTER = %f,%f",centre.latitude,centre.longitude);
}
the problem is that when I switch to the UIViewController that contains MKMapView it gives MAP CENTER = 0.000000,0.000000 for two times then gives the actual coordinates MAP CENTER = 55.755786,37.617633. I want the actual coordinates as soon as I switch to that UIViewController.

Is the coordinates (55.755786,37.617633) your current location ?
MKMapView takes some time to get a lock on GPS to fetch the coordinates for your current location. Until then centerCoordinate might return (0,0)

Try this this may help you.
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

Related

How to Get MapView Center Latitude and Longitude Every time when Mapview are Swiped in ios?

I'm using the Google Map SDk for showing location to a user in my app. In my app I want to center the latitude and longitude to the user every time they swipe/zoom or change the map.
For that I put one center location in my GMSMapView using the following code:
- (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
}
But this is not giving me the center location and latitude every time. How can I fix this?
The documentation on didChangeCameraPosition here:
https://developers.google.com/maps/documentation/ios/reference/protocol_g_m_s_map_view_delegate-p#aabd01d59d7680799a0c24d3c8b5e4622
Says this:
This may not be called for all intermediate camera positions. It is always called for the final position of an animation or gesture.
Set google map delegate first
mapview.deligate=self;
this will give you centre coordinate

iOS: Best way to position my mapView to show the coordinates at the top left

I have the following code below that positions my mapView where it shows the coordinates in the center. However, I need it to position the mapView where it will show my coordinates at the top left. The code below is what I am currently using. I am using the category called MKMapView+ZoomLevel.h. If you need any more information please let me know.
[self.mapView setCenterCoordinate:coordinate zoomLevel:level animated:animated];
Get the region with self.mapView.region and find a new coordinate using some simple geometry. Region will give you the center point and the width and height of the view in degrees of latitude and longitude.
From there, you can determine a center coordinate to provide to the map view that will set your original coordinate in the upper left corner.
CLLocationCoordinate2DMake(self.mapView.coordinate.latitude +
self.mapView.region.span.latitudeDelta / 2,
self.mapView.coordinate.longitude +
self.mapView.region.span.longitudeDelta / 2)

Make MKMapView only zoom in on the centerCoordinate?

I have a MKPinAnnotationView that is always in the center of the map. When panning and zooming
the pin gives me the center coordinates (lat/long) of the map.
Currently when you zoom in, it just zooms into wherever your directing the map to zoom into.
I'd really like to lock the zoom onto the pin.
Any ideas on how I'd achieve this?
Assuming by MKPinAnnotation you mean MKPinAnnotationView, you can access the annotation's coordinate and use that to create a region and subsequently set the region of the mapView to that region centered on the coordinate:
MKCoordinateRegion region = MKCoordinateRegionMake(pin.annotation.coordinate, MKCoordinateSpanMake(.05, .05));
[self.mapView setRegion:region animated:YES];

Display the map scale in mapkit

I am working with mapkit in Xcode 5.1 and am trying to display the map scale in regionDidChangeAnimated. I have no idea now to accomplish this though. I tried to look around and was unsuccessful. Any ideas?
EDIT:
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
CLLocationCoordinate2D neCoord;
neCoord = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D swCoord;
swCoord = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];
CLLocationDistance distance = [neCoord distanceFromLocation:swCoord];
}
Any reason why I am getting an error with the last line, CLLocationDistance?
Use the methods that translate between the coordinates on the map and the points on the map view, such as convertPoint:toCoordinateFromView:. Use the edge points of your view for this.
Now you have the coordinates - you can calculate the distances between the points with CLLocation's distanceFromLocation:. You can make some assumptions about the width of your view based on the physical properties of, day an iPhone or iPad and calculate the scale.

MKMapView Region

I have a question regarding setting the region on my MKMapView.
I need to set the mapview to display a specific region when my view first loads.
The north east and south west latitude and longitude of this region is:
North East Coordinate Lat:59.623724 Long:2.911587
South West Coordinate Lat:49.004833 Long:-11.361825
Further to this, I would like to 'lock' the mapview to this region. Ideally the lock will be transparent, i.e: the coordinates above represent the maximum extent of the MKMapView. However if it is simply a case of checking the northeast and southwest coordinates within
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated
and resetting the view if they exceed my maximum range, that would be acceptable to me also.
Many thanks for any pointers on this matter.
EDIT:
Regarding the first part of my question, I have figured out I can set the initial region on the MKMapView using the following code:
CLLocationCoordinate2D neCoord;
neCoord.latitude = 59.787643;
neCoord.longitude = 3.025857;
CLLocationCoordinate2D swCoord;
swCoord.latitude = 49.394171;
swCoord.longitude = -11.036642;
MKCoordinateRegion region;
region.center.latitude = neCoord.latitude - (neCoord.latitude - swCoord.latitude) * 0.5;
region.center.longitude = neCoord.longitude + (swCoord.longitude - neCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(neCoord.latitude - swCoord.latitude); // Add a little extra space on the sides
region.span.longitudeDelta = fabs(swCoord.longitude - neCoord.longitude); // Add a little extra space on the sides
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
First, you'll need to make sure you set the region on the map view after the view has been displayed. If you set it before the map has loaded, it probably won't center on that region. Once you've done that, just set self.mapView.zoomEnabled = NO; and self.mapView.scrollEnabled = NO; and it will prevent the user from moving the map around.
If you want to lock the maximum bounds the user can view but still allow scrolling and zooming, you will have to use -mapView:regionDidChangeAnimated: and 'bump' the user back inside your bounds if they leave it. Note that the user experience for this will probably suck - they'll pan around, let go, and then the map will suddenly move back to the region you defined. You could try using -mapView:regionWillChangeAnimated: and modify the map region if they left your boundaries, that could be a little less jarring.

Resources