Mapkit ios6 example.

Discussion in 'iOS Programming' started by RookieAppler, Nov 6, 2012.

  1. RookieAppler, Nov 6, 2012
    Last edited by a moderator: Nov 6, 2012

    RookieAppler macrumors member

    Joined:
    Mar 15, 2012
    #1
    Hello,
    I am using the Mapkit in iOS6. I added the viewwillappear method and passed my current co-ordinates.
    Code:
    #define METERS_PER_MILE 160.344
    - (void)viewWillAppear:(BOOL)animated
    {
    	CLLocationCoordinate2D zoomLocation;
    	zoomLocation.latitude = XX.657016;
    	zoomLocation.longitude = -XXX.369874;
    	
    	MKCoordinateRegion viewRegion  = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5 *METERS_PER_MILE, 0.5*METERS_PER_MILE);
    	
    	[_mapView setRegion:viewRegion animated:YES];
    	_mapView.showsUserLocation = YES;
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    
    }
    Inside the above "didUpdateLocations" method i am logging the co-ordinates and it is showing the co-ordinates pertaining to sanfransisco , CA. That is NOT my current location. Why is it showing that location. I set the location coordinates and still it shows different place.WHat am i doing wrong? I am new to iOS and objective c.What it shows on the simulator is the world map and a blue dot at sanfransisco,CA. Can someone explain me how it should be done to get my current location?
    Also i have 3 screens on my app. Second screen has the mapkit at the bottom left about the size of rubik's cube (3X3). So when i click a button on second screen and move to third screen( do my work on 3rd screen and click back) and come back to second screen it shows the location correctly(2nd screen->3rd screen ->2nd Screen).But there is no blue dot on it.Why?IF more information is needed please let me know. Thanks...
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    This is in the Simulator, correct? It defaults to Apple Headquarters in Cupertino. If you want to change the location, visit the Debug > Location menu item.
     
  3. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #3
    @dejo. Thanks.Yes,its in simulator. I did Debug->Location->menu item and changed to the given list there. It changes to each one correctly. How can i add my current location to it. Because it is recognizing this location, but
    on back operation.

    Screen1->Screen2(mapkit is on this screen)->Screen3.

    So when i go back from screen 3 to screen 2 ..it shows:)))..but not from Screen1 ->Screen2:)(). Please help.
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Um, did you not notice the Custom Location... option? Use it to set it to your current location.
     
  5. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #5
    Also, just one point, you have METERS_PER_MILE as 160.3. Shouldn't it be 1609.34? Just something to notice
     
  6. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #6
    @Dejo. I cannot see a custom location option. All i can see are 12 locations London...brazil..and a add a GPX file. Where is it?Can you please point it to me?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    You don't see this (from the iOS Simulator NOT Xcode)?:
     

    Attached Files:

  8. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #8
    @Dejo.Thanks.That was awesome.Now, i have another question. So i was referring to another problem also. So on my first load i.e i run the app,move from 1st screen to 2nd screen, the mapkit now shows my current location on a zoomed-out mode(World map), with a blue dot. But i go back to 1st screen and come back to 2nd screen, now this time it shows my location in zoomed in mode. What i mean it shows the two streets and a blue dot where i am right now. But on the second time. Why does it not show the first time? Am i missing an order of functions or something. Did it ever happen to you?If you have any information about this please share..Thanks again.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    No, that has never happened to me. Are you sure that viewWillAppear: is being called correctly the first time (since that's where you set your map's view region)?
     
  10. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #10
    @Dejo_Ok. So, i checked the working of how the screen loads. First viewDidLoad is called then viewWillAppear and then didUpdateLocations is called.
    viewDidLoad ->ViewWillAppear->didUpdateLocations.
    But i set the co-ordinates from the simulator like you said. Now eventhough i set them at runtime i want them to be set in ViewWilAppear. So that when i change the location to somewhere in viewwillappear it should point to that location. Also,zoom in mode. Is there any property that one has to set.Well it doesnt make sense, because on coming to the 2nd screen second time it zooms in to my location. So there is something missing on the first load.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Are you saying that you want the map to show the region around the current user's location, wherever that may be, when the screen first appears?

    If so, that's not always possible, because it takes a while for the location manager to triangulate the user's position and the screen may already have appeared. You would want to delay setting of the view region until you have sufficient accuracy in the user's location then.

    If not, please describe in more detail what you are trying to do. I'm guessing that English may not be your first language and, if so, it may be harder for you to describe it properly.

    Without seeing actual compiling code (no "zoomLocation.latitude = XX.657016;", for example) for viewWillAppear: and locationManager:didUpdateLocations: and anything else that we might need to see, I can't say what might be wrong.

    P.S. Did you fix the issue with your value for METERS_PER_MILE yet?
     
  12. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #12
    @Dejo. Yes. I want to show show the region around the current user's location, wherever that may be, when the screen first appears. Yes i fixed the meters issue. And yes, English is not my first language. :D
    Code:
    #define METERS_PER_MILE 1609.344
    - (void)viewWillAppear:(BOOL)animated
    {
    	CLLocationCoordinate2D zoomLocation;
    	zoomLocation.latitude = XX.657016;
    	zoomLocation.longitude = -XXX.369874;
    	
    	MKCoordinateRegion viewRegion  = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5 *METERS_PER_MILE, 0.5*METERS_PER_MILE);
    	
    	[self.mapView setRegion:viewRegion animated:YES];
    	self.mapView.zoomEnabled = YES;
    	self.mapView.showsUserLocation = YES;
    }
    - (void)viewDidLoad
    {
    	self.locationManager = [[CLLocationManager alloc] init];
    	self.locationManager.delegate = self;
    	self.locationManager.distanceFilter = kCLDistanceFilterNone;
    	self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    	[self.locationManager startUpdatingLocation];
    }
    - (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
    {
    	MKAnnotationView *annotationView = [views objectAtIndex:0];
    	id <MKAnnotation> mp = [annotationView annotation];
    	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate] ,500,500);
    	
    	[mv setRegion:region animated:YES];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    	 	
    	NSString *latLongValue=@"";
    	int  s=0;
    	
    	CLLocation *location = [locations objectAtIndex:0];
    	NSLog(@"%f%f",location.coordinate.latitude,location.coordinate.longitude);
    	 
    	int degrees = location.coordinate.latitude;
    	double decimal = fabs(location.coordinate.latitude - degrees);
    	int minutes = decimal *60;
    	double seconds = decimal * 3600-minutes * 60;
    		
    	NSString *lat = [NSString stringWithFormat:@"%d %d' %1.4f\"",degrees,minutes,seconds];
    	s = seconds;
    	latLongValue = [NSString stringWithFormat:@"%d %d %d %@",degrees,minutes,s,@" "];
    	NSLog(@"so far string is %@",latLongValue);
     	self.txtLatitude.text = latLongValue;
    	NSLog(@"lat is %@",lat);
    	degrees = location.coordinate.longitude;
    	decimal = fabs(location.coordinate.longitude - degrees);
    	minutes = decimal *60;
    	seconds = decimal *3600 - minutes *60;
    	NSString *longt = [NSString stringWithFormat:@"%d %d' %1.4f\"",degrees,minutes,seconds];
    	s =seconds;
        NSString *longtValue= [NSString stringWithFormat:@"%d %d %d",degrees,minutes,s];
    	self.txtLongitude.text = longtValue;
    	latLongValue = [latLongValue stringByAppendingString:longtValue];
    	NSLog(@"the final value is %@",latLongValue);
    	NSLog(@"long is %@",longt);
    	 		
    	[self.locationManager stopUpdatingLocation];
    }
    
    These are the places where the mapkit is used in my entire view.m file.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Code:
    zoomLocation.latitude = XX.657016;
    zoomLocation.longitude = -XXX.369874;
    These lines for sure don't compile. Give us real code that compiles.
     
  14. RookieAppler thread starter macrumors member

    Joined:
    Mar 15, 2012
    #14
    @Dejo.
    You can use your co-ordinates and check. If it works for you then please tell me where you made the changes.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Doesn't matter. The point is when those lines are executed, you probably don't yet know what the user's current location is. So, you can't set them in a meaningful way. Once you do know the user's location, with a certain amount of accuracy, then you can set the view region.

    Now, what would be a good candidate delegate method that lets you know when the location manager has updated?
     

Share This Page