Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 6, 2012, 06:54 PM   #1
RookieAppler
macrumors member
 
Join Date: Mar 2012
Mapkit ios6 example.

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...

Last edited by dejo; Nov 6, 2012 at 07:23 PM. Reason: Please use [code] tags.
RookieAppler is offline   0 Reply With Quote
Old Nov 6, 2012, 07:25 PM   #2
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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.
__________________
dejo is offline   0 Reply With Quote
Old Nov 6, 2012, 07:37 PM   #3
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
RookieAppler is offline   0 Reply With Quote
Old Nov 6, 2012, 07:44 PM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by RookieAppler View Post
How can i add my current location to it.
Um, did you not notice the Custom Location... option? Use it to set it to your current location.
__________________
dejo is offline   0 Reply With Quote
Old Nov 7, 2012, 06:54 AM   #5
waterskier2007
macrumors 68000
 
waterskier2007's Avatar
 
Join Date: Jun 2007
Location: White Lake, MI
Send a message via AIM to waterskier2007
Also, just one point, you have METERS_PER_MILE as 160.3. Shouldn't it be 1609.34? Just something to notice
__________________
2012 Mac Mini, 2.6 GHz, 16GB RAM, 1TB HDD
2.4Ghz 15" Macbook Pro
16 GB iPhone 5 : 32 GB iPhone 4S : 16 GB iPad 3
16 GB Nexus 7 (2012)
waterskier2007 is offline   0 Reply With Quote
Old Nov 7, 2012, 09:44 AM   #6
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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?
RookieAppler is offline   0 Reply With Quote
Old Nov 7, 2012, 09:58 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by RookieAppler View Post
@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?
You don't see this (from the iOS Simulator NOT Xcode)?:
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-11-07 at 8.57.28 AM.png
Views:	22
Size:	119.0 KB
ID:	375643  
__________________
dejo is offline   1 Reply With Quote
Old Nov 7, 2012, 11:50 AM   #8
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
RookieAppler is offline   0 Reply With Quote
Old Nov 7, 2012, 12:08 PM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by RookieAppler View Post
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?
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)?
__________________
dejo is offline   0 Reply With Quote
Old Nov 7, 2012, 01:07 PM   #10
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
RookieAppler is offline   0 Reply With Quote
Old Nov 7, 2012, 01:26 PM   #11
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by RookieAppler View Post
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.
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.

Quote:
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.
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?
__________________
dejo is offline   0 Reply With Quote
Old Nov 7, 2012, 02:25 PM   #12
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@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.
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.
RookieAppler is offline   0 Reply With Quote
Old Nov 7, 2012, 02:40 PM   #13
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Code:
zoomLocation.latitude = XX.657016;
zoomLocation.longitude = -XXX.369874;
These lines for sure don't compile. Give us real code that compiles.
__________________
dejo is offline   0 Reply With Quote
Old Nov 7, 2012, 04:27 PM   #14
RookieAppler
Thread Starter
macrumors member
 
Join Date: Mar 2012
@Dejo.
You can use your co-ordinates and check. If it works for you then please tell me where you made the changes.
RookieAppler is offline   0 Reply With Quote
Old Nov 7, 2012, 04:46 PM   #15
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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?
__________________
dejo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
coordinates, ios6, location, map, mapkit

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
General: Upgrade iPad 3 from iOS6.0 to iOS6.1, then JB? kandavu Jailbreaks and iOS Hacks 5 Jan 15, 2014 01:08 AM
iPhone: Restore SMS Database from iOS6.1.2 to iOS6.0.2 HeyGreggie Jailbreaks and iOS Hacks 12 Apr 8, 2013 11:40 AM
MapKit leaking Stefan51278 iPhone/iPad Programming 0 Dec 9, 2012 05:56 PM
how to upgrade from ios6 beta4 to released ios6? kkam iOS 6 2 Sep 19, 2012 07:44 PM
transition from iOS6 beta 4 to iOS6 on Sep 21st ???? Krissypantz2828 iOS 6 18 Sep 13, 2012 04:25 AM

Forum Jump

All times are GMT -5. The time now is 10:33 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC