views:

63

answers:

2

Hi Guys,

I am a newbie in MapKit for iPhone and tried to implement this, for some reason I can't see the current location blue dot, anyone else had this issue????

#import "DetailMapViewController.h"
#import "mapAnnotations.h"

@implementation DetailMapViewController

@synthesize inStock;

-(void)getlocation:(CLLocationCoordinate2D)loc
{
    location = loc;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.navigationItem.title = @"Street View";
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.delegate=self;      
    //MKCoordinateRegion region;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 5000, 5000);

    mapAnnotations *ann = [[mapAnnotations alloc] init];
    ann.title = @"";
    ann.subtitle = @"";
    ann.coordinate = region.center;

    mapView.showsUserLocation = YES;
    [mapView addAnnotation:ann];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    [self.view insertSubview:mapView atIndex:0];
}


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    if (annotation == mapView.userLocation)
    {

        annView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"blueDot"];
        if (annView != nil)
        {
            annView.annotation = annotation;
        }
        else
        {
            annView = [[[NSClassFromString(@"MKUserLocationView") alloc] initWithAnnotation:annotation reuseIdentifier:@"blueDot"] autorelease];


        }
    }

    if([inStock isEqual:@"yes"]){
        annView.pinColor = MKPinAnnotationColorGreen;
    } 
    if([inStock isEqual:@"no"]){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    if([inStock isEqual:@"unknown"]){

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
        [annView addSubview:imageView];




    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

- (void)dealloc {
    [super dealloc];
}


@end
A: 

Are you running this inside the Simulator? In that case your current location is Infinite Loop, Cupertino.

Paul Ardeleanu
Yeah but it still doesnt showthe current location
Are the location services enabled? In 4.0 or later you can check the value of [CLLocationManager locationServicesEnabled]
Paul Ardeleanu
yeah its enabled
A: 

The way the viewForAnnotation is currently written, it should actually crash when trying to show the current location because the "blue dot" annotation view doesn't have the pinColor or animatesDrop properties.

Try changing it to the following:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if ([annotation isKindOfClass:MKUserLocation.class]) {
        //user location view is being requested,
        //return nil so it uses the default which is a blue dot...
        return nil;
    }

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];

    if([inStock isEqual:@"yes"]){
        annView.pinColor = MKPinAnnotationColorGreen;
    } 
    if([inStock isEqual:@"no"]){
        annView.pinColor = MKPinAnnotationColorRed;
    }
    if([inStock isEqual:@"unknown"]){
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"greyPin.png"]];
        [annView addSubview:imageView];
    }
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

On the simulator, the user's location will be Cupertino, CA, USA (a little south of San Francisco). If your own annotation is not within 5000 meters of that, you won't see the blue dot. You'll have to zoom out to see it.

aBitObvious
Doesnt work... Thanks