I'm new to iOS map-kit, even though i have loaded mapview in my app using the following code
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
// 4
[_mapView setRegion:adjustedRegion animated:YES];
I want to allow user only to view a certain region not the whole world in the map, For example i take america, i want to allow user to view only america in the map, where he can't able to view any other places. What have to do to achieve this.. Needs your help.
You need to set the zoom level to a particular country or a particular region..... i think you must have a look at this.... that helped me when i was trapped in such kind of confusion... i hope that might help you too....
Related
I am using following codes
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), Some KM, Some KM);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
Otherway around is
MKCoordinateRegionMake(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), ADD SPAN HERE)
Both of these makes the map zoom. How is it possible that I change the Region without any zoom.
Get the current region and just change the center point.
// get current region
MKCoordinateRegion region = self.mapView.region;
// Update the center
region.center = CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04);
// apply the new region
self.mapView.region = region;
If you have a coordinate to set, use the MKMapView -setCenterCoordinate:animated: method. Animating the position change gives the user a clue about what's happened.
CLLocationCoordinate2D coordinate = annView.annotation.coordinate;
[myMapView setCenterCoordinate:coordinate animated:YES];
Also, no need to make a new coordinate, just use the one already in the annotation view.
I wanna zoom into the user current location when the app starts in the MapKit.
This is my code (in the viewDidLoad function):
Locate=[[CLLocationManager alloc]init];
Locate.delegate=self;
Locate.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
Locate.distanceFilter=kCLDistanceFilterNone;
[Locate startUpdatingLocation];
[super viewDidLoad];
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=0.3f;
span.longitudeDelta=0.3f;
//Set Region
myRegion.center=center;
myRegion.span=span;
[_MyMap setRegion:myRegion animated:YES];
Also I implemented the didUpdateLocation function with the same code as previous.
The problem is that when the user location is changing (when the user is moving) the screen makes zoom at him but I can't move the screen, if I try to move it return to the user location immediately, so I can't see the whole map.
For Zooming the map you have to change the region values means center and span.once see this one Mapview Zoom
in my case i have used this one for moving the map when i click on particular button.
MKCoordinateSpan span = MKCoordinateSpanMake(30.5982f,0.0001f);
CLLocationCoordinate2D coordinate = {36, 90};
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
[self.mapView setRegion:regionThatFits animated:YES];
I solved this issue by adding touchesMoved function and I stopped updating the Location in this function.
Solved.
I'm trying to center my mapView on the user's location, but an exception is thrown that's caught by the AppDelegate before the mapView or view controller are even loaded.
mapView is an MQMapView
userLocation is assigned earlier from mapView.userLocation.location.coordinate
MQCoordinateSpan userSpan = MQCoordinateSpanMake(1000, 1000);
MQCoordinateRegion userRegion = MQCoordinateRegionMake(userLocation, userSpan);
[mapView setRegion:userRegion animated:true];
As far as I can make out from the MapQuest developer guide I'm calling setRegion correctly. Any idea what might be causing the exception?
I was having crashes with my iPhone4S and console revealed nan values for region. After trying about 7 different solutions from SO and various suggestions from Apple DTS, I solved it by eliminating the regionThatFits call. I simply used:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, visibleDistance, visibleDistance);
[_mapView setRegion:adjustedRegion animated:YES];
Apparently there is a problem with that regionThatFits method.
This code sets a default zoom level centered around a specified location in viewDidLoad.
The code works fine in previous versions of iOS:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
.
.
.
[mapView setRegion:adjustedRegion animated:NO];
However, in iOS6 for locations with latitude above ~ 75 (>75.1) the app crashes with the following message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Invalid Region <center:nan, nan span:nan, nan>'
I found that for the given zoom level mapView can't set a proper MKCoordinateRegion internally. [mapView regionThatFits:region] returns all values as nan. If I use the region variable directly, it just shows the default map (the whole world).
After some testing I found that by adjusting the visibleDistance I can get the code to work properly. The magic distance seems to be slightly above 20 kilometers (somewhere between 22 and 23 km for a series of latitudes and latitudeDelta values).
This happens only on northern latitudes (-80 works just fine).
The maps work at any location after the initial positioning. It looks like Apple changed the way visible map regions are initialized. I'm using a higher zoom level for the affected region as a workaround. Is there any other way to make it work properly?
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
.
.
.
[mapView setRegion:adjustedRegion animated:NO];
It will work..
CLLocationCoordinate2D southwest, northeast;
southwest.latitude = 34.172684;
southwest.longitude = -118.604794;
northeast.latitude = 34.236144;
northeast.longitude = -118.500938;
BSForwardGeocoderCoordinateBounds *bounds = [BSForwardGeocoderCoordinateBounds boundsWithSouthWest:southwest northEast:northeast];
try this....
I was having crashes with my iPhone4S and console revealed nan values for region. After trying about 7 different solutions from SO and various suggestions from Apple DTS, I solved it by eliminating the regionThatFits call. I simply used:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, visibleDistance, visibleDistance);
[_mapView setRegion:adjustedRegion animated:YES];
Apparently there is a problem with that regionThatFits method.
I found a version of this code on a Chinese website and it seems to work for me. He is only bypassing sizeThatFits when the NAN is returned, thus only adjusting if necessary, and if this bug gets fixed by Apple (assuming it is a bug) then it won't be an issue at all.
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, mapSizeMeters, mapSizeMeters);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
if (isnan(adjustedRegion.center.latitude)) {
// iOS 6 will result in nan. 2012-10-15
adjustedRegion.center.latitude = viewRegion.center.latitude;
adjustedRegion.center.longitude = viewRegion.center.longitude;
adjustedRegion.span.latitudeDelta = 0;
adjustedRegion.span.longitudeDelta = 0;
}
[mapView setRegion:adjustedRegion animated:YES];
I have an app which is providing a zoomable MKMapView to the user. I want to be able to store the user's preferred coordinates and zoom level for when the map view is first displayed.
Currently, in viewDidLoad, I am providing a default set of coordinates and zoom level for the initial map presentation:
zoomLocation.latitude = 55.50;
zoomLocation.longitude = -5.50;
// specify size of region to display
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 340.0*METERS_PER_MILE, 340.0*METERS_PER_MILE);
// auto adjust region to fit screen
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
// display the new region
[mapView setRegion:adjustedRegion animated:YES];
What I am trying to do is, when the user scrolls and zooms the map to their preferred default view, they can hit the "Set Default" button and store the required properties to implemented in future whenever the view loads.
To store the coordinates of the user's selected view, I have this:
// gets coordinates of currently viewed map image
CGPoint pointCentrePoint = CGPointMake(mapView.frame.size.width/2, mapView.frame.size.height/2);
centrePoint = [mapView convertPoint:pointCentrePoint toCoordinateFromView:mapView];
NSLog(#"LAT: %f LON: %f", centrePoint.latitude, centrePoint.longitude);
What I'm struggling with is how to store the user's selected MKCoordinateRegion, or zoom level. Is there a way I can access this property for the current view, so it can be reused in future when the view loads?
Unlike the JavaScript Google Maps api, the MKMapView doesn't have a readily available "zoom level" property (it isn't really necessary for your purpose).
Work with the center and span values in the region property.
This question gives an example of how to save/load the region to NSUserDefaults.