Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-04-2011, 06:42 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
MrPeanutz90 is on a distinguished road
Exclamation Current Location Button

Hi All,

I need some help/advice on the userLocation function. I'm not sure how to use that function on a button. Currently, i could see my current location on a map view. I've created a custom button called UserClickCLButton. If the user were to scroll elsewhere on the map, the user would be animated back to the current location blue dot when the button is clicked.

I need some help for my coding, any help would be great! Thanks!
The following below is my code:

-(IBAction) UserClickCLButton
{
MKCoordinateRegion newRegion;

newRegion.center. = ?
OR
userLocation = ?

newRegion.span.latitudeDelta = 0.23;
newRegion.span.longitudeDelta = 0.23;

[mapView setRegion:newRegion animated:YES];
}
MrPeanutz90 is offline   Reply With Quote
Old 01-04-2011, 07:30 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by MrPeanutz90 View Post
Hi All,

I need some help/advice on the userLocation function. I'm not sure how to use that function on a button. Currently, i could see my current location on a map view. I've created a custom button called UserClickCLButton. If the user were to scroll elsewhere on the map, the user would be animated back to the current location blue dot when the button is clicked.

I need some help for my coding, any help would be great! Thanks!
The following below is my code:

-(IBAction) UserClickCLButton
{
MKCoordinateRegion newRegion;

newRegion.center. = ?
OR
userLocation = ?

newRegion.span.latitudeDelta = 0.23;
newRegion.span.longitudeDelta = 0.23;

[mapView setRegion:newRegion animated:YES];
}
You've got the right idea.

You need to have a location manager active and generating location updates. When you receive a location update through locationManager:didUpdateToLocation:fromLocation:, save the location information in an instance variable.

Then, when the user clicks the "show current location" button, use the saved lat/long in your code above.

An alternative to this would be to ask your map view for it's userLocation property, and then get the coordinate property of the user location.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 01-04-2011, 09:46 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
MrPeanutz90 is on a distinguished road
Exclamation Current Location Button

Quote:
Originally Posted by Duncan C View Post
You've got the right idea.

You need to have a location manager active and generating location updates. When you receive a location update through locationManager:didUpdateToLocation:fromLocation:, save the location information in an instance variable.

Then, when the user clicks the "show current location" button, use the saved lat/long in your code above.

An alternative to this would be to ask your map view for it's userLocation property, and then get the coordinate property of the user location.
Thanks for the comment, so do I just need to use back the current "locationManager:didUpdateToLocation:fromLocation: "
which is in my code already? how do I save the information? Is it something like "region.center = newLocation.coordinate" below?

So after that I would just place my button method, "(IBAction) UserClickCLButtton" after the locationManager method?

//Current locationManager updating the map

- (void) locationManagerCLLocationManager *) manager
didUpdateToLocationCLLocation *) newLocation
fromLocationCLLocation *) oldLocation {
//---update the map---
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = .025;
span.longitudeDelta = .025;

region.center = newLocation.coordinate;
region.span = span;
[mapView setRegion:region animated:YES];
}

I would like to know about the alternative where I could use the map view's userLocation property. Do I need to declare the MKUserLocation class on the .h file? And then in my .m file, I would declare it as mapView.userLocation = userLocation.coordinate?
MrPeanutz90 is offline   Reply With Quote
Old 01-04-2011, 10:00 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by MrPeanutz90 View Post
Thanks for the comment, so do I just need to use back the current "locationManager:didUpdateToLocation:fromLocation: "
which is in my code already? how do I save the information? Is it something like "region.center = newLocation.coordinate" below?

So after that I would just place my button method, "(IBAction) UserClickCLButtton" after the locationManager method?

//Current locationManager updating the map

- (void) locationManagerCLLocationManager *) manager
didUpdateToLocationCLLocation *) newLocation
fromLocationCLLocation *) oldLocation {
//---update the map---
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = .025;
span.longitudeDelta = .025;

region.center = newLocation.coordinate;
region.span = span;
[mapView setRegion:region animated:YES];
}

I would like to know about the alternative where I could use the map view's userLocation property. Do I need to declare the MKUserLocation class on the .h file? And then in my .m file, I would declare it as mapView.userLocation = userLocation.coordinate?

I told you how to save the location. Create an instance variable (called something like currentCoordinate) in your map view delegate of type CLLocationCoordinate2D.

In your didUpdateToLocation method, save the newLocation.coordinate to currentCoordinate.

Finally, in your code that handles the button press, use currentCoordinate as the region center that you pass to setRegion.

Slow down, read the documentation on MKMapView and CLLocationManager, and look through some of the sample apps that come with XCode. It's very well documented, and there are a number of good examples apps included. It takes some time and study to understand how it works and how to use it.

You might want to download and try our company's location bookmarking app, Save My Place. (The iPhone version is only $0.99.) In our app, we have a checkbox that lets you ask to show your current location. When you check it, the app scrolls the map when the user's location gets too close to the edge of the map. It will also scroll the map to keep both your current location and your selected destination on-screen, if you ask to show both. We do all that by setting flags based on the state of "show location" and "show destination" checkboxes, and then deciding how to respond to location manager updates based on the state of the flags and the current map span.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.

Last edited by Duncan C; 01-04-2011 at 10:06 AM.
Duncan C is offline   Reply With Quote
Old 01-06-2011, 09:42 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 4
MrPeanutz90 is on a distinguished road
Exclamation

Quote:
Originally Posted by Duncan C View Post
I told you how to save the location. Create an instance variable (called something like currentCoordinate) in your map view delegate of type CLLocationCoordinate2D.

In your didUpdateToLocation method, save the newLocation.coordinate to currentCoordinate.

Finally, in your code that handles the button press, use currentCoordinate as the region center that you pass to setRegion.

Slow down, read the documentation on MKMapView and CLLocationManager, and look through some of the sample apps that come with XCode. It's very well documented, and there are a number of good examples apps included. It takes some time and study to understand how it works and how to use it.

You might want to download and try our company's location bookmarking app, Save My Place. (The iPhone version is only $0.99.) In our app, we have a checkbox that lets you ask to show your current location. When you check it, the app scrolls the map when the user's location gets too close to the edge of the map. It will also scroll the map to keep both your current location and your selected destination on-screen, if you ask to show both. We do all that by setting flags based on the state of "show location" and "show destination" checkboxes, and then deciding how to respond to location manager updates based on the state of the flags and the current map span.
Hi Duncan,

I've followed your instructions and created my instance variable, currentCoordinate (CLLocationCoordinate2D) inside my .h file.

@interface MapView : UIViewController
<CLLocationManagerDelegate, MKMapViewDelegate, MKReverseGeocoderDelegate>
{
IBOutlet UITextField *accuracyTextField;
IBOutlet UITextField *latitudeTextField;
IBOutlet UITextField *longitudeTextField;
CLLocationManager *lm;

CLLocationCoordinate2D currentCoordinate;

}

AND THEN, I save the newLocation.coordinate to currentCoordinate as you've stated.


- (void) locationManagerCLLocationManager *) manager
didUpdateToLocationCLLocation *) newLocation
fromLocationCLLocation *) oldLocation {

newLocation.coordinate = currentCoordinate;

AND LASTLY, I use the currentCoordinate as the newRegion.center, is that right?

-(IBAction) UserClickCLButton
{

MKCoordinateRegion newRegion;

newRegion.center = currentCoordinate;

newRegion.span.latitudeDelta = 0.23;
newRegion.span.longitudeDelta = 0.23;

[mapView setRegion:newRegion animated:YES];
}

BUT, it stills shows me and error stated :

newLocation.coordinate = currentCoordinatel -"object cannot be set - either readonly property or no setter found"
MrPeanutz90 is offline   Reply With Quote
Old 01-06-2011, 10:37 AM   #6 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 410
timle8n1 is on a distinguished road
Default

Dude use CODE tags!

Code:
- (void) locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation {
  currentCoordinate = newLocation.coordinate
}
timle8n1 is offline   Reply With Quote
Reply

Bookmarks

Tags
button, current location

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 364
7 members and 357 guests
blueorb, fredidf, iAppDeveloper, iGamesDev, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:30 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0