How to animate MapView once it loads? - iphone

Could someone please help me with loading maps using animation? I am trying to load a MapView. I want it to zoom to my place once the view loads. I am trying this:
- (void)viewDidAppear {
[super viewDidAppear:TRUE];
MKCoordinateRegion region;
CLLocationCoordinate2D location=secondMap.userLocation.coordinate;
MKCoordinateSpan span;
location.latitude=40.443242;
location.longitude=-79.94305;
region.span=span;
region.center=location;
[secondMap setRegion:region animated:TRUE];}
This code does not seem to work. When I tried, viewDidLoad, the location was already there, which I think is expected.
Thanks.

your viewDidAppear function is missing parameter
-(void)viewDidAppear:(BOOL)animated;

Related

MKMapViewprevent user from zooming out past a certain range

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;

How to know current Location in iPhone?

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.

Center MKMapView on userLocation (initially) - only works on iPhone 4?

I implemented the following MKMapView method, which runs after annotations are added. I have my MKMapView map (parishMap) set to "Shows user location" in Interface Builder, and upon loading the mapview, the blue dot always appears within a second or so on the mapview.
Here's the code I have in my Map View controller implementation:
// Center map on user location (initially)
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == parishMap.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.03;
span.longitudeDelta=0.03;
CLLocationCoordinate2D location=parishMap.userLocation.coordinate;
location = parishMap.userLocation.location.coordinate;
region.span=span;
region.center=location;
[parishMap setRegion:region animated:TRUE];
[parishMap regionThatFits:region];
}
}
}
When I open the mapview on my iPhone 4, the mapview always (and almost immediately) moves to center on the userLocation dot. However, when I open the mapview on an iPhone 3Gs or iPod Touch (2010), it doesn't center on the userLocation, but just stays at the default zoom/region I set for the map view.
Any ideas as to why this code won't work for non-iPhone 4 devices (they're all running 4.2.x latest iOS)?
This is probably an issue with the amount of time it takes for the GPS to lock a position on these different devices.
I'd recommend using CLLocationManager and its delegate methods to center the map.
You can start updating location by creating an instance of CLLocationManager, setting its delegate property to self and then implementing:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
Where you can center your map accordingly based on the latest location update.
have you tried using this code to center the map:
[mapView setCenterCoordinate:location animated:YES];
I'm not sure why it would work for iPhone 4 and not the others, but this is the code I use to adjust the region:
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.03, 0.03);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[mapView setRegion:adjustedRegion animated:YES];
Try it that way and see how that works. The way you're doing the regionThatFits method, that's actually not supposed to be how the method is used. You can try commenting out that last line of your code, it should make no difference. Anyway, try it with the method I just gave and let me know if you have any questions.

Zoom on userLocation

how can I zoom the map on my userLocation automatically in my app?
I have the following code to zomm in the map but i must zoom on the userLocation and the following code zooms always to africa?
MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.2;
zoomIn.span.longitudeDelta *= 0.2;
zoomIn.center.latitude = mapView.userLocation.location.coordinate.latitude;
zoomIn.center.longitude = mapView.userLocation.location.coordinate.longitude;
[mapView setRegion:zoomIn animated:YES];
OK i solved the problem, with the following delegate Method:
-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation){
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.9;
span.longitudeDelta=0.9;
CLLocationCoordinate2D location =mv.userLocation.coordinate;
location = mv.userLocation.location.coordinate;
region.span = span;
region.center = location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
thanks alot mate, this helped me greatly. Really wanted to vote but cant because of my rep level.
I also found that using:
mapView.userLocation.location.coordinate.latitude;
always zoomed me to africa, and never centered me onto my current location.
But by using the delegate method:
-(void)mapView:(MKMapView *)myMapView didAddAnnotationViews:(NSArray *)views {
I was able to solve this successfully. What I discovered is that when using mapView.userLocation.location.coordinate.latitude; in the viewDidLoad method wasn't triggering the correct user coords on startup of the view, hence it was displaying the wrong coords.

how i get map for particular area?

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...