views:

528

answers:

2

hellow all, 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;
}
+1  A: 

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
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 notthanks ....
ratnasomu
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
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
I have edited my answer, check it.
Warrior
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 <MKAnnotation>)annotation {}in the above method , how we can identify which is of current location and which is of the location we are showing..
ratnasomu
see this stack overflow answer http://stackoverflow.com/questions/1853144/showsuserlocation-returns-pin-instead-of-blue-dot-in-iphone-simulator
Warrior
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
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *defaultPinID = @"CameraAnnotation"; MKPinAnnotationView *retval = nil; static int postag=1;
ratnasomu
(MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID]; if(annotation=MapView.userLocation) { return nil; } // If we have to, create a new viewelse if (retval == nil) { retval = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease]; UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; myDetailButton.frame = CGRectMake(0, 0, 50, 23);
ratnasomu
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
sorry for confused code posted ..
ratnasomu
please put the above code in your question itself in well formatted manner
Warrior
i have edited my question you can check it now
ratnasomu
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
oh, Thanks alot for your cooperation in finishing off this things
ratnasomu
You are always welcome.Enjoy coding.All the best.
Warrior
thank you ..Yes I do enjoy the code
ratnasomu
A: 

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

Kevin
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