I am not really too sure how to word this into google, so I will attempt to ask it here. I am attempting to use the MapKit to display different routes on a map for only one city. Is there a way to limit the amount of the World Map that can be used in the API?
Any suggestions are appreciated.
if you add this to viewDidLoad:
MKCoordinateRegion region={{0.0,0.0,},{0,0.0}};
region.center.latitude = 30.44;
region.center.longitude = -84.31;
region.span.latitudeDelta=0.03;
region.span.longitudeDelta=0.03;
[mapView setRegion:region animated:YES];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:NO];
[mapView setDelegate:self];
it will center around the lat and long, with the magnification specified by the latDelta and longDelta. The lower those values are, the closer you will be zoomed.
MKMapView will notify its delegate whenever the map is scrolled (via delegate methods -mapView:regionWillChangeAnimated: and
– mapView:regionDidChangeAnimated:). Perhaps you could write some code in those methods to check and see whether the user has scrolled outside of the city, and if so, use setRegion:animated: to move the map back into the correct bounds.
Related
I have a lot of pin annotations on the MKMapView in my app, the iPhone gets very slow and unresponsive when a lot of them are in view on the map. I would like the user to be able to zoom, but not out past a certain level, such as 2km squared or something.
Here's what I've got:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[BicycleLDNService sharedService] requestLocationForClient:self];
CLLocationCoordinate2D zoomLocation;
CLLocation *deviceLocation = [[BicycleLDNService sharedService] deviceLocation];
zoomLocation.latitude = deviceLocation.coordinate.latitude;
zoomLocation.longitude = deviceLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*kMetresPerKilometre, 0.5*kMetresPerKilometre);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.zoomEnabled = YES;
}
Is there some sort of property or delegate method I can employ? Couldn't find anything useful on google or here!
Thanks in advance!
The span defines how much of the map at the given point should be visible and is also how you set the zoom level.
You can access this by using
region.span.latitute=0.5;
region.span.longitude=0.6;
Check the zoom level of the map and then set the zoomEnabled Property NO.
mapView.zoomEnabled=NO;
I want to know is it possible to show only specific region on map not the full world map using Map Kit.
Like if i want to show Asia map in my application then map kit hides remaining part of the map.
To handle the "map kit hides remaining part of the map" requirement, one thing you can do is create a black polygon overlay that covers the whole world with a cutout over Asia (or wherever you like).
For example, where you initialize the map (eg. in viewDidLoad):
CLLocationCoordinate2D asiaCoords[4]
= { {55,60}, {55,150}, {0,150}, {0,60} };
//change or add coordinates (and update count below) as needed
self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];
CLLocationCoordinate2D worldCoords[4]
= { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
MKPolygon *worldOverlay
= [MKPolygon polygonWithCoordinates:worldCoords
count:4
interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
//the array can have more than one "cutout" if needed
[myMapView addOverlay:worldOverlay];
and implement the viewForOverlay delegate method:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
pv.fillColor = [UIColor blackColor];
pv.alpha = 1.0;
return pv;
}
return nil;
}
This looks like this:
If you also want to restrict the user from scrolling beyond Asia or zooming too far out, then you'll need to do that manually as well. One possible way is described in Restrict MKMapView scrolling. Replace theOverlay in that answer with asiaOverlay.
You can specify the region as an MKCoordinateRegion and then tell an MKMapView instance to only show that region using the setRegion and regionThatFits message.
Alternatively you could use the visibleMapRect property instead of the region. This might better fit your needs.
In short read the MKMapView Class Reference document from Apple.
Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion.
- (void) goToLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
region.center=locationToShow;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}
In my app user is trying to see the number of activity in a given country. Let's say if he wants to see all the activities in US region so how should i set the focus on US only and not on the whole world.
Thanks,
Use Following Method:
-(void)setMapCenter:(CLLocationCoordinate2D)location
{
NSLog(#"Current Location : %f, %f",location.latitude,location.longitude);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
//location.latitude=(float *)(self.appDelegate.currentLocationLatitude);
//location.latitude=self.appDelegate.currentLocationLongitude;
region.center=location;
[self._mapView setRegion:region animated:TRUE];
[self._mapView regionThatFits:region];
}
And Call this method through following line in any event:
[self setMapCenter:countryLocation.coordinate];
All you have to do is figure out the coordinates that you want the map to centre on, and how large the map size is, and that will work. I'm not sure if there are any online resources which have generic points for each country, but you could look them up on google, and the make a dictionary which holds all of those centre points and sizes for each country, with the name of the country being the key for those points. Then, when the user selects a country, you look it up in the dictionary, and load those centre points and sizes into your map, and that will display that country. Hope that helps!
friends,
i am new to iphone development and just implement google map in iphone application.
now i am facing a problem with following description
i have list of latitude and longitude values when display them on map those pins are displayed on different locations.
now i want to set zoom level so that all pins are displayed on the screen.
any help would be appreciated.
If you're using an MKMapView you can use the setRegion:animated: method to change the region displayed. I would use annotationsInMapRect: to determine which annotations are being shown. Since annotationsInMapRect: returns an NSSet you could make an NSSet of all your annotations, and zoom out the MKMapView until the NSSet returned from annotationsInMapRect: matches your NSSet of all annotations.
The values 3.0 and 2.0 are only examples you can increase or decrease the values according to your requirement(how much you have to zoom) ,map is my mkmapview object
MKCoordinateSpan span;
span.latitudeDelta = 3.0;
span.longitudeDelta = 2.0;
MKCoordinateRegion region;
region.span = span;
[map setRegion:region animated:YES];
[map regionThatFits:region];
I don't know about map-view.I want map for particular area for that what to do?if it is possible then give me the answer with example and code.
If you are asking about Google Map in iPhone SDK 3.0 then you need to look at MKCoordinateRegion and MKCoordinateSpan in MapKit framework.
You can set the co-ordinates of your map view by setting MKCoordinateRegion and zoom level by using latitudeDelta and longitudeDelta property of MKCoordinateSpan.
for example,
CLLocationCoordinate2D location;
location.latitude=yourlatitude;
location.longitude=yourlongitude;
span.latitudeDelta=0.01; //or whatever zoom level
span.longitudeDelta=0.01;
region.span=span;
region.center=location;
[mapview setRegion:region animated:TRUE];
[mapview regionThatFits:region];
Hope that helps...