Search code examples
ioslocationmkmapviewsubviewanimated

Show user selected location on mapview


I am getting co-ordintes from user in UITextFields. He enters them in degrees,minutes and seconds. I get the latitude and longitude from them after conversion. I set the coordinates and "build" a map. This builded map view I add it to the UIViewController as subview.

CLLocationCoordinate2D location = CLLocationCoordinate2DMake(finalLatValue,finalLongValue);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, LOCATION_DISTANCE, LOCATION_DISTANCE);
MKMapView *mvMap = [[MKMapView alloc] initWithFrame:CGRectMake(20,594,360,347)];

mvMap.mapType = MKMapTypeSatellite;
mvMap.autoresizingMask = UIViewAutoresizingNone;
mvMap.region = region;
[mvMap setRegion:region animated:YES];
//mvMap.showsUserLocation = YES;
[self.view addSubview:mvMap];

So as you can see I create a location using finalLatValue and finalLongValue.Then I create a map and adding it to the self.view as a subview. It works perfectly. Now all I need is a blue dot on that location. I did setRegion:animated and showUserLocation..Both did not work.What am I missing? The idea is for the user to enter co-ordinates in Degrees,minutes and seconds and show the location on the map with a blue dot.CGRectMake(20,594,360,347) just creates my map view at the bottom left side of the view,as I have buttons and labels all over my view. If more information is needed please ask.Thanks..

MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = location;
annotationPoint.title=@"Company";
annotationPoint.subtitle = @"Company HQ";
[mvMap addAnnotation:annotationPoint];

So i added these lines and now my location shows a red pin. That will do for now. Now i have question about zoom in and zoom out of the map-view with pinch. How can i do it?Any help will be appreciated. I have more questions.


Solution

  • There is a good documentation from Apple

    The LocationAwareness Programming Guide

    http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/MapKit/MapKit.html

    There you find under chapter Anotations:

    The blue dot is called 'UserLocAnnotation`` (or similar)