How to know current Location in iPhone? - iphone

I am new to iphone App development, I Need code Current Location of the user. I want show his Location in My App using map Kit. Please tell me how to do that?

You may refer the apple's document for this CLLocationmanager class reference

- (IBAction) showCurrentLocation{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.007;
span.longitudeDelta=0.007;
CLLocationCoordinate2D location = [[[mapview userLocation] location] coordinate];
//NSLog(#"Location found from Map: %f %f",location.latitude,location.longitude);
region.span=span;
region.center=location;
[mapview setRegion:region animated:TRUE];
[mapview regionThatFits:region];
}

You need to tell the map view to show the user's location (it's not on by default). Add this to the viewDidLoad:
[mapView setShowsUserLocation:YES];
Note that in the simulator, the user location is always Cupertino, CA so you might not see it depending on the map's current center and zoom.

Related

how to display all PlaceMarks/pins on google map in iphone?

friends,
i am new to iphone development and just implement google map in iphone application.
now i am facing a problem with following description
i have list of latitude and longitude values when display them on map those pins are displayed on different locations.
now i want to set zoom level so that all pins are displayed on the screen.
any help would be appreciated.
If you're using an MKMapView you can use the setRegion:animated: method to change the region displayed. I would use annotationsInMapRect: to determine which annotations are being shown. Since annotationsInMapRect: returns an NSSet you could make an NSSet of all your annotations, and zoom out the MKMapView until the NSSet returned from annotationsInMapRect: matches your NSSet of all annotations.
The values 3.0 and 2.0 are only examples you can increase or decrease the values according to your requirement(how much you have to zoom) ,map is my mkmapview object
MKCoordinateSpan span;
span.latitudeDelta = 3.0;
span.longitudeDelta = 2.0;
MKCoordinateRegion region;
region.span = span;
[map setRegion:region animated:YES];
[map regionThatFits:region];

How to animate MapView once it loads?

Could someone please help me with loading maps using animation? I am trying to load a MapView. I want it to zoom to my place once the view loads. I am trying this:
- (void)viewDidAppear {
[super viewDidAppear:TRUE];
MKCoordinateRegion region;
CLLocationCoordinate2D location=secondMap.userLocation.coordinate;
MKCoordinateSpan span;
location.latitude=40.443242;
location.longitude=-79.94305;
region.span=span;
region.center=location;
[secondMap setRegion:region animated:TRUE];}
This code does not seem to work. When I tried, viewDidLoad, the location was already there, which I think is expected.
Thanks.
your viewDidAppear function is missing parameter
-(void)viewDidAppear:(BOOL)animated;

iphone zoom to user location on mapkit

I'm using map kit and showing user's location using "showsUserLocation"
I"m using following code to zoom to user's location, but not zooming. Its zooming to some location in Africa, though the user location on map is showing correct.
MKCoordinateRegion newRegion;
MKUserLocation* usrLocation = mapView.userLocation;
newRegion.center.latitude = usrLocation.location.coordinate.latitude;
newRegion.center.longitude = usrLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 20.0;
newRegion.span.longitudeDelta = 28.0;
[self.mapView setRegion:newRegion animated:YES];
Why is user's location correctly showing and not zooming properly. Can some one correct me please?
mapView.userLocation is set to a location off the coast of Africa (0,0) when first instantiated. As a result, I do something like the following, which causes the zoom to occur when the annotation appears. Of course, this is just an example:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
you can try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
UserTrackingMode will zoom to user Location.
Have you verified that the location returned by mapView.userLocation isn't at 0, 0?

Zoom on userLocation

how can I zoom the map on my userLocation automatically in my app?
I have the following code to zomm in the map but i must zoom on the userLocation and the following code zooms always to africa?
MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.2;
zoomIn.span.longitudeDelta *= 0.2;
zoomIn.center.latitude = mapView.userLocation.location.coordinate.latitude;
zoomIn.center.longitude = mapView.userLocation.location.coordinate.longitude;
[mapView setRegion:zoomIn animated:YES];
OK i solved the problem, with the following delegate Method:
-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation){
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.9;
span.longitudeDelta=0.9;
CLLocationCoordinate2D location =mv.userLocation.coordinate;
location = mv.userLocation.location.coordinate;
region.span = span;
region.center = location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
thanks alot mate, this helped me greatly. Really wanted to vote but cant because of my rep level.
I also found that using:
mapView.userLocation.location.coordinate.latitude;
always zoomed me to africa, and never centered me onto my current location.
But by using the delegate method:
-(void)mapView:(MKMapView *)myMapView didAddAnnotationViews:(NSArray *)views {
I was able to solve this successfully. What I discovered is that when using mapView.userLocation.location.coordinate.latitude; in the viewDidLoad method wasn't triggering the correct user coords on startup of the view, hence it was displaying the wrong coords.

how i get map for particular area?

I don't know about map-view.I want map for particular area for that what to do?if it is possible then give me the answer with example and code.
If you are asking about Google Map in iPhone SDK 3.0 then you need to look at MKCoordinateRegion and MKCoordinateSpan in MapKit framework.
You can set the co-ordinates of your map view by setting MKCoordinateRegion and zoom level by using latitudeDelta and longitudeDelta property of MKCoordinateSpan.
for example,
CLLocationCoordinate2D location;
location.latitude=yourlatitude;
location.longitude=yourlongitude;
span.latitudeDelta=0.01; //or whatever zoom level
span.longitudeDelta=0.01;
region.span=span;
region.center=location;
[mapview setRegion:region animated:TRUE];
[mapview regionThatFits:region];
Hope that helps...