2

whenever we load a mapview ,it automaticallay shows the current user location right. In my case i am showing another location on map ,such that the map loads the current location and zoomed to the location i have given .. but, the current location point was not showing (the blue one which comes automaticay..). i have given mapView.showCurrentLocation=TRUE; but its not showing . so could any one tells the way it works and it should say the current location mark and then zoomed to the point i have given. Thanks

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *defaultPinID = @"CameraAnnotation";
    MKPinAnnotationView *retval = nil;
    static int postag=1;

    (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if(annotation=MapView.userLocation)
    {
        return nil;
    }
    // If we have to, create a new view
    else    if (retval == nil)
    {
        retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        myDetailButton.frame = CGRectMake(0, 0, 50, 23);
        myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        myDetailButton.tag=postag;
        postag++;
        [myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside];
        retval.rightCalloutAccessoryView = myDetailButton;
        [retval setPinColor:MKPinAnnotationColorRed];
        retval.animatesDrop = YES;
        retval.canShowCallout = YES;
    }

    return retval;
}
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
ratnasomu
  • 139
  • 3
  • 9

2 Answers2

2

In simulator the default current location is Cupertino.This is what you are seeing, it will work properly in device.

More over you should use CLLocationManager to get the current location.

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; // Tells the location manager to send updates to this object
[locationManager startUpdatingLocation];
}

To change the pin color

MKPinAnnotationView   *pin=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]autorelease];

[pin setPinColor:MKPinAnnotationColorRed];

All the best.

Warrior
  • 39,156
  • 44
  • 139
  • 214
  • thanks for your response but i using that only for showing other location . but my problem here is the map is directly zooming into the location .so current location is not visible directly . i am trying to increase the span values some how achieved but dnt knw it is correct soln or not thanks .... – ratnasomu May 07 '10 at 09:36
  • see this link to work with zoom level. http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/ . If you are going to show only one location in maps, u can adjust in manually the span values depending on your requirement. – Warrior May 07 '10 at 09:52
  • thank you , will work with it.I am struck up with another issue currently.that is i was locating one more location along with current location here and adding annotations for the locations such that the user will go to another screen when click the annotation. But my problem is the annotation is coming to the current location also .. i think u got my problem and pin color also i need to show for them diferently(red for custom pin and blue for user current location ) .. sorry for trouble, and thanks alot – ratnasomu May 07 '10 at 10:22
  • Thnks alot Warrior for your response and patience. your answer is right, but you did not understand my requirement i want to customize the view.that is the current location should be in blue color pin and other one in red color. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation { } in the above method , how we can identify which is of current location and which is of the location we are showing.. – ratnasomu May 07 '10 at 11:37
  • see this stack overflow answer http://stackoverflow.com/questions/1853144/showsuserlocation-returns-pin-instead-of-blue-dot-in-iphone-simulator – Warrior May 07 '10 at 11:54
  • thanks warrior , the answer is correct one . In my case if i have done the accessory button for other location also not visible. my code is – ratnasomu May 07 '10 at 12:48
  • - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation { static NSString *defaultPinID = @"CameraAnnotation"; MKPinAnnotationView *retval = nil; static int postag=1; – ratnasomu May 07 '10 at 12:48
  • (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if(annotation=MapView.userLocation) { return nil; } // If we have to, create a new view else if (retval == nil) { retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; myDetailButton.frame = CGRectMake(0, 0, 50, 23); – ratnasomu May 07 '10 at 12:49
  • myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; myDetailButton.tag=postag; postag++; [myDetailButton addTarget:self action:@selector(showLinks:) forControlEvents:UIControlEventTouchUpInside]; retval.rightCalloutAccessoryView = myDetailButton; [retval setPinColor:MKPinAnnotationColorRed]; retval.animatesDrop = YES; retval.canShowCallout = YES; } return retval; } – ratnasomu May 07 '10 at 12:50
  • please put the above code in your question itself in well formatted manner – Warrior May 07 '10 at 12:53
  • hai Warrior, this thing is finished now itself I went in reverse procedure, i.e,rather than returning nil when it is userlocation ,instead i did annotation changes when it is not current location .. so returning nil.. – ratnasomu May 07 '10 at 13:19
  • oh, Thanks alot for your cooperation in finishing off this things – ratnasomu May 07 '10 at 13:20
1

take a look at the answer on this question to set the MkMapView zoom level to encompass all the MKAnnotations attached to it

Community
  • 1
  • 1
Kevin
  • 2,810
  • 1
  • 23
  • 19
  • thanks,i have set with by increasing span value of the location that i am showing expect current location. some how they are near. – ratnasomu May 07 '10 at 11:47