Solved

Need help displaying GPS location on MAP

Posted on 2010-08-25
4
1,775 Views
Last Modified: 2013-11-25
I'm not to iPhone development... so I'm still learning...

I'm creating a simple iPhone app to display my current location on the map.

I'm using this tutorial to see how I can display the map on the iPhone... but the location (lat, long) is hard coded.

http://blancer.com/tutorials/i-phone/76877/mapkit-example-in-iphone/

Everything works fine...

Now I found another tutorial that gets your location using the GPS..

http://www.vellios.com/2010/08/16/core-location-gps-tutorial/


My question is, since in the second tutorial they display the information on labels I was wondering how can I pass that information back to the map.

Here is the code that I need to update. How can I create a variable to replace the region information with the data collected from the iPhone GPS...

Thanks...

- (void)viewDidLoad {

    [super viewDidLoad];

       

        [mapView setMapType:MKMapTypeStandard];

        [mapView setZoomEnabled:YES];

        [mapView setScrollEnabled:YES];

        MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };

        region.center.latitude = 22.569722 ;

        region.center.longitude = 88.369722;

        region.span.longitudeDelta = 0.01f;

        region.span.latitudeDelta = 0.01f;

        [mapView setRegion:region animated:YES];

       

        [mapView setDelegate:self];

       

        DisplayMap *ann = [[DisplayMap alloc] init];

        ann.title = @" Kolkata";

        ann.subtitle = @"Mahatma Gandhi Road";

        ann.coordinate = region.center;

        [mapView addAnnotation:ann];

}

Open in new window

0
Comment
Question by:Medrise
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:tisaksen
Comment Utility
You need to do that in the CLLocationManagerDelegate method locationManager:didUpdateToLocation:fromLocation:

Then just use the data you get in that method to create your region struct.
0
 

Author Comment

by:Medrise
Comment Utility
Thanks, I will try that later today...
0
 

Author Comment

by:Medrise
Comment Utility
I moved the information to the locationManager:didUpdateToLocation

but now I get the world map, and not the location I need.

Here is the code.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	//GET CURRENT LOCATION
	CLController = [[coreLocationController alloc] init];
	CLController.delegate = self;
	[CLController.locMgr startUpdatingLocation];
}

- (void)locationUpdate:(CLLocation *)location {
	//latitudeLabel = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
	//longitudeLabel = [NSString stringWithFormat:@"%f", location.coordinate.longitude];
	
	[mapView setMapType:MKMapTypeStandard];
	[mapView setZoomEnabled:YES];
	[mapView setScrollEnabled:YES];
	
	MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
	
	region.center.latitude = location.coordinate.latitude;
	region.center.longitude = location.coordinate.longitude;
	region.span.longitudeDelta = 1.04*(126.766667 - 66.95);
	region.span.latitudeDelta = 1.04*(49.384472 - 24.520833);
	
	[mapView setRegion:region animated:YES];
	
	[mapView setDelegate:self];
	
	displayMap *ann = [[displayMap alloc] init];
	ann.title = @" Kolkata";
	ann.subtitle = @"Mahatma Gandhi Road";
	ann.coordinate = region.center;
	
	[mapView addAnnotation:ann];
}

Open in new window

0
 
LVL 2

Accepted Solution

by:
tisaksen earned 500 total points
Comment Utility
I guess locationUpdate is called by the delegate method I mentioned.
Anyway try this:

#define kZoomRadius 1000
// create map region
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, kZoomRadius, kZoomRadius);
// for correcting map aspect view
MKCoordinateRegion adjustRegion = [mapView regionThatFits:region];
// finally set the map region
[self.mapView setRegion:adjustRegion animated:YES];
            
where kZoomRadius is in meters
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article guides you how to create Excel readable XML format using xcode. Here are some points you must take care - First create a NSMutableString variable NSMutableString *excel = [[NSMutableString alloc] init]; and  then build up your …
Let Bitmoji into your life. Now is the time to learn a new language of smartphone messaging with this brief introduction.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now