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

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.

Related

How to display bubble on map annotation pin automatically without drop animation

I have a standart annotation pin, placing on my coordinates, automatically showing bubble with Title/Subtitle. When tracking my coordinates in move, the pin every time does drop animation. I tried to annView.animatesDrop=FALSE but in this case bubble doesn't appear automatically (for automatic appearance I used selectAnnotation:addAnnotation animated:YES). What needs to do to have a pin with automatic Title/Subtitle showing without drop animation?
P.S. Sorry for my bad English.
annView.animatesDrop=NO;
It works in my code. Displays the pin without drop animation.
You have to use the annView.animatesDrop=FALSE but at the right place. That would be in the
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
delegate method, right after you've created that annotationView.
You cannot select the annotationView in the same run loop you add it, if I remember well. I use a
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
to do it, setting the delay to 0 to have it occur on the new run loop.
In my case, I have
[self performSelector:#selector(selectPlace:) withObject:annotation afterDelay:0];
- (void)selectPlace:(id)<MKAnnotation>place{
//Lots of stuff for my precise case
CLLocationCoordinate2D center;
center.latitude = place.latitudeValue;
center.longitude = place.longitudeValue;
MKCoordinateSpan span;
span.latitudeDelta = 4;
span.longitudeDelta = 5;
MKCoordinateRegion germany;
germany.center = center;
germany.span = span;
[mapView setRegion:germany animated:YES];
// End of custom stuff
[mapView selectAnnotation:(id)place animated:YES]; //This is what you're interested in
}

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.

making mapview work like maps application iphone sdk

I have a normal mapView, which can tell the users location, but when users location is located my map doesn't suddenly zoom in to the users location like Apples maps application.
Does anybody know how to implement this feature!
Implement the didUpdateUserLocation delegate method:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region;
region.center = userLocation.location.coordinate;
MKCoordinateSpan span = { 1.0, 1.0 };
region.span = span;
[mapView setRegion:region animated:YES];
}
Make sure your map view's delegate is set.

iphone zoom to user location on mapkit

I'm using map kit and showing user's location using "showsUserLocation"
I"m using following code to zoom to user's location, but not zooming. Its zooming to some location in Africa, though the user location on map is showing correct.
MKCoordinateRegion newRegion;
MKUserLocation* usrLocation = mapView.userLocation;
newRegion.center.latitude = usrLocation.location.coordinate.latitude;
newRegion.center.longitude = usrLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 20.0;
newRegion.span.longitudeDelta = 28.0;
[self.mapView setRegion:newRegion animated:YES];
Why is user's location correctly showing and not zooming properly. Can some one correct me please?
mapView.userLocation is set to a location off the coast of Africa (0,0) when first instantiated. As a result, I do something like the following, which causes the zoom to occur when the annotation appears. Of course, this is just an example:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
you can try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
UserTrackingMode will zoom to user Location.
Have you verified that the location returned by mapView.userLocation isn't at 0, 0?

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.