MKMapView Trouble - iphone

I am having trouble when trying to use a MKMapView. This is my first time trying to use one of these and I haven't been able to figure out how to work it. Here are the two different pieces of sample code that I used to try to get it to work and neither work:
mapView = [[MKMapView alloc] initWithFrame:CGRectMake( 0, 0, 320, 150 )];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance( CLLocationCoordinate2DMake( latitude, longitude ), metersPerMile*0.5, metersPerMile*0.5 );
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
[mapView setRegion:adjustedRegion];
or
mapView = [[MKMapView alloc] initWithFrame:CGRectMake( 0, 0, 320, 150 )];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance( CLLocationCoordinate2DMake( latitude, longitude ), metersPerMile*0.5, metersPerMile*0.5 );
[mapView setRegion:region];
or
mapView = [[MKMapView alloc] initWithFrame:CGRectMake( 0, 0, 320, 150 )];
[self.mapView setRegion:MKCoordinateRegionMake(CLLocationCoordinate2DMake( latitude, longitude ), MKCoordinateSpanMake( 0.01, 0.01 ))];
all of these snippets of code do absolutely nothing for the MKMapView. Whenever the view ends up loading, it doesn't do anything and I'm just left looking at all of North America, which isn't very helpful.
If anyone can help me out with this, I would greatly appreciate it.

You haven't added map to your view.. like [self.view addSubView:mapView];

I show the user where he is by implementing – mapView:viewForAnnotation: from MKMapViewDelegate. Take a look at this:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// If it's the user location, return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
{
MKCoordinateSpan coordinateSpan = MKCoordinateSpanMake(0.009, 0.009);
[mapView setRegion:MKCoordinateRegionMake(annotation.coordinate, coordinateSpan) animated:YES];
}
return nil;
}

If you are tying to load the MapView this will help
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSLog(#"map loading...");
}

Related

place pin on mapview by coordinates sent from appdelegate

This takes me days..I could not make it work.
I write a method that places a pin on map as below:
- (void) SetMaps:(NSString *)Lats :(NSString *)lons;
{
if([upLo isEqualToString:#"Y"])
{
NSLog(#"setting maps:%#,%#",Lats,lons);
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
  [mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [Lats doubleValue] ;
region.center.longitude = [lons doubleValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = region.center;
[self.mapView addAnnotation:point];
}
}
This method runs well if I call it in secondViewController.m in a tab bar controll application.
But, I want to call it from appdelegate.m.
So in appdelegate.m, I put
secondViewController *Gper=[[secondViewController alloc]init];
[Gper SetMaps:LAT:LON];
[Gper release];
from this NSLog(#"setting maps:%#,%#",Lats,lons); I can see the lat and lons values are correct into this method.
However, the map doesnt change to this location.
Whatelse should I do to make it showing new location?
Thanks for any help.
I feel you should learn (Objective-)C memory management and Cocoa conventions better before doing anything further. + alloc returns a new instance of your view controller class, and not the one that is currently displayed.
And begin your class names with a capital letter. And your method names with a lowercase one.

How to find if mapview pins are overlapping and zoom?

I want to zoom-in if there are multiple pins that overlapping and if they are not just want to show some detail page.
I've came up with a solution but it works most of the time but not always
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
MKMapPoint annotationPoint = MKMapPointForCoordinate(view.annotation.coordinate);
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, (mRect.size.width/view.frame.size.width)*[self.mapView currentZoomLevel]*10 , (mRect.size.height/view.frame.size.height)*[self.mapView currentZoomLevel]*10 );
NSLog(#"point rect origin %f ..%f.....size %f....%f",pointRect.origin.x, pointRect.origin.y, pointRect.size.width, pointRect.size.height);
NSSet * AnnSet = [self.mapView annotationsInMapRect:pointRect];
NSLog(#"annotations obtained :%i",[[AnnSet allObjects] count]);
// [self.mapView setVisibleMapRect:pointRect animated:YES];
NSLog(#"max zoom level :%i",[self.mapView currentZoomLevel]);
if ([[AnnSet allObjects] count]>1 && [self.mapView currentZoomLevel]<19 ) {//zoom it
[self.mapView setCenterCoordinate:view.annotation.coordinate zoomLevel:[self.mapView currentZoomLevel]+1 animated:YES];
[self.mapView deselectAnnotation:view.annotation animated:NO];
}
else{
//Show detail page
}
}
problem seems to be how i'm generating pointRect.
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, (mRect.size.width/view.frame.size.width)*[self.mapView currentZoomLevel]*10 , (mRect.size.height/view.frame.size.height)*[self.mapView currentZoomLevel]*10 );
any kind of help is welcome :)

User location annotation misplaced when not default

I am showing the user location in an MKMapView. I don't want the default blue circle but instead the red pin.
Therefore I implemented mapView:viewForAnnotation: and made it always return a red pin as the user's location is the only pin I will show on the map view.
When using the default blue circle the location is quite precise but when changing to the red pin it's off quite a lot. It's as if the red pin is dropped precisely and is then moved up so it does not longer show my current position.
This is my code:
- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
NSLog(#"MapView: Annotation.");
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
[pin setPinColor:MKPinAnnotationColorRed];
[pin setAnimatesDrop:YES];
[pin setCanShowCallout:NO];
return pin;
}
I use mapView:didAddAnnotationViews: to center the map view to my current location.
- (void)mapView:(MKMapView *)_mapView didAddAnnotationViews:(NSArray *)views
{
for(MKAnnotationView *annotationView in views)
{
if(annotationView.annotation == _mapView.userLocation)
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.20;
span.longitudeDelta = 0.20;
CLLocationCoordinate2D location = [[_mapView userLocation] coordinate];
region.span = span;
region.center = location;
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
}
}
Does anyone know why my annotation is no longer placed correct when using the red pin instead of the default annotation view?
Setting [pin setAnimatesDrop:NO] instead of YES solved this.

MapView not zooming to user location

My mapview is not zooming to my user location. Please can you point out the error:
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] init];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self performSelector:#selector(startupZoom) withObject:nil afterDelay:1.25];
}
- (void)startupZoom {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=mapView.userLocation.coordinate.latitude;
location.longitude=mapView.userLocation.coordinate.longitude;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
NSLog(#"%f, %f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);
}
The mapView is being created but its frame is not set and it's not being added as a subview of the view controller's view.
Change the alloc+init line to (change the dimensions as needed):
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
Then before the performSelector line, add this:
[self.view addSubview:mapView];
Note however that it's better to use the MKMapView's didUpdateUserLocation delegate method to zoom to the user's location instead of assuming the user location will be available after some arbitrary, fixed interval like 1.25 seconds.
For example, remove the performSelector line and implement didUpdateUserLocation:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self startupZoom];
}
Only possible problem using the didUpdateUserLocation method is that it will be called every time the user's location changes so if you only want to zoom once on startup, you'll need to add a BOOL flag ivar and check/set it accordingly.

Image instead of a the default pin in iPhone's MapKit framework?

Is it possible to have our own image instead of the default pin in MapKit map on iPhone?
I am working on an application which would show friends' locations much like Google Latitude and need to show image of friends at their locations.
It is possible using the JavaScript Google Map but i want to know if someone can give some sample code for MapKit based map.
Yes it is possible.
For that u have to use MKAnnotationView instead of MKPinAnnotationView.
and do not use annotation.animatesDrop property.
Here are the sample code you can use in viewForAnnotation,
annotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"try"];
annotation.canShowCallout = YES;
annotation.image = [UIImage imageNamed:#"image.png"];
return annotation;
You can also set the frame of the image. For that in above code we have to make this simple changes.
UIImage *pinImage = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:pinImage] autorelease];
imageView.frame = CGRectMake(-20, 0, 40, 30);
[annotation addSubview:imageView];
And we have to comment the line
// annotation.image = [UIImage imageNamed:#"image.png"];
By using span property you can easily zoom to your require
MKCoordinateSpan span;
MKCoordinateRegion region;
mapView.scrollEnabled=YES;
span.latitudeDelta = 100.0;//more value you set your zoom level will increase
span.longitudeDelta =100.0;//more value you set your zoom level will increase
mapView.showsUserLocation=YES;
region.span = span;
region.center = from.coordinate;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
[mapView addAnnotation:from];