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];
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;
Is it possible using MapKit to show a map in iPhone, like i´m doing, to display zoom controls on the map?
If not, what are the flags or methods i should use to increase and diminish the zoom, so i can created methods which will do this at the push of a button.
There aren't any built-in controls for it.
A couple of suggestions:
You can predefine some spans and store them in an array somewhere and then store your position in that array as well. Zooming in/out changes the position and gets the span.
Zooming in takes the current span and divides by 2, zooming out multiplies by 2.
You change the span in the region of the mapView. So you have to:
Get the region of the mapView.
Get the span of the region.
Change the span to what you want.
Set the regions span to the new span.
Set the mapView's region to the new region.
Here's some code for suggestion 2:
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta = region.span.latitudeDelta*2;
span.longitudeDelta = region.span.longitudeDelta*2;
region.span = span;
[mapView setRegion:region animated:TRUE];
For swift 4, this is what I use:
func zoom(_ zoomin : Bool) {
var region = mapView.region;
var span = MKCoordinateSpan();
span.latitudeDelta = zoomin ? region.span.latitudeDelta / 2 : region.span.latitudeDelta * 2;
span.longitudeDelta = zoomin ? region.span.longitudeDelta / 2 : region.span.longitudeDelta * 2;
region.span = span;
mapView.setRegion(region, animated: true);
}
Off the top of my head I don't remember (at work on a windows box) if there is a switch to display the zoom controls. I assume you are talking about displaying the level of zoom, because by default you can pinch/spread the view for zoom.
If there isn't a switch to display the controls, then you will need to create a custom view layer and put it on top of the mapkit view. Then you will need to call the different functions to change the zoom level.
Those functions are all documented in the mapkit docs. Just do a search for MapKit in the documentation center.
Edit:
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205
According to the docs there isn't a switch to display the controls, but the zoom enabled property lets you turn the ability on and off.
So you can subscribe to the delegate function regionWillChange and use the mapView object to get the zoom level and then set your graphics accordingly.
I don't think you should do that, the expected way to zoom in and out is using the pinch gestures. However if you still want to go ahead and do it, you should place yourself the buttons over the MKMapView, and then with those modify the region property of the MKMapView
I am new to iphone App development, I Need code Current Location of the user. I want show his Location in My App using map Kit. Please tell me how to do that?
You may refer the apple's document for this CLLocationmanager class reference
- (IBAction) showCurrentLocation{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.007;
span.longitudeDelta=0.007;
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
//NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
region.span=span;
region.center=location;
[mapview setRegion:region animated:TRUE];
[mapview regionThatFits:region];
}
You need to tell the map view to show the user's location (it's not on by default). Add this to the viewDidLoad:
[mapView setShowsUserLocation:YES];
Note that in the simulator, the user location is always Cupertino, CA so you might not see it depending on the map's current center and zoom.
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.
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...