How get latiude and longitude of point? - annotations

I am developing an iPhone Application in which i have provide Pin Annotation button on tool bar. When user click on button then pin will drop on center of map view. Now i want that user move that pin and place at their desired place and i will get latitude and longitude of that point. So how i define all these event with my map view? How do that? I get this code for add annotation.
- (void)addPinAnnotation:(id)sender {
UICRouteAnnotation *pinAnnotation = [[[UICRouteAnnotation alloc] initWithCoordinate:[routeMapView centerCoordinate]
title:nil
annotationType:UICRouteAnnotationTypeWayPoint] autorelease];
[routeMapView addAnnotation:pinAnnotation];
}
How get latitude and longitude of that point?
Thanks in advance....

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Add annotation to map
MKCoordinateRegion reg;
CLLocationCoordinate2D location = newLocation.coordinate ;
reg.center = location;
MKCoordinateSpan span;
span.latitudeDelta = 1.5;
span.longitudeDelta = 1.5;
reg.span = span;
reg.center=newLocation.coordinate;
[self.mapView setRegion:reg animated:TRUE];
annotation = [[myAnnotation alloc] initWithCoordinate:newLocation.coordinate title:#"Click > to set or drag to move"];
[self.mapView addAnnotation:annotation];
[self.mapView selectAnnotation:annotation animated:YES];
[manager stopUpdatingLocation];
[loading stopAnimating];
}

Related

MKMapView not zooming to region

I have an MKMapView, and I'm trying to set the map, so that when you first load the application it goes to a user set location, and if the user hasn't set one, to a default one. The problem is that it always seems to go to 0 latitude, 0 longitude.
-(void) viewDidLoad {
[worldView setShowsUserLocation:YES];
double longitude = [[NSUserDefaults standardUserDefaults] doubleForKey:WhereamiNewLongPrefKey];
double latitude = [[NSUserDefaults standardUserDefaults] doubleForKey:WhereamiNewLatPrefKey];
CLLocationCoordinate2D savedLoc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(savedLoc, 250, 250);
NSLog(#"latitude :%f", region.center.latitude);
NSLog(#"longitude :%f", region.center.longitude);
[worldView setRegion:region animated:YES];
}
I've tried setting setShowUserLocation to NO, but that doesn't work. I know it's reading the correct region, because the NSLog is outputing the default latitude, but the map insists on going to somewhere in China...
I've also tried setting the region in mapView:didUpdateUserLocation:, but still the same result.
What am I doing wrong?
EDIT: I added the NSLog to output the longitude as well. The output is:
2012-11-14 09:50:30.699 Whereami[34256:13d03] latitude :20.516700
2012-11-14 09:50:30.699 Whereami[34256:13d03] longitude :99.900000
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = 20.516700;
theCoordinate.longitude = 99.900000;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(theCoordinate, 1000, 1000);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];

Crashed while removing the pin from mapview

I am working on MapView,on which I have two buttons.
1)centreButton:
this button drops the pin annotation at the centre of the current map.
when this button is pressed, I am storing the last annotation in a NSMutable array.
then remove the last annotation from mapview and drop one pin at the centre of map
Code I have done for this part is as follows:
function for dropping the pin
- (void)PinDropwithlatitude:(double)lat longitude:(double)longi droptitle:(NSString *)title
{
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = lat;
theCoordinate.longitude = longi;
MKCoordinateRegion region;
region.center.latitude = theCoordinate.latitude;
region.center.longitude = theCoordinate.longitude;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta =0.005;
span.longitudeDelta =0.005;
region.span = span;
[MapView setRegion:region animated:YES];
SetLat =lat;
SetLong =longi;
DDAnnotation *annotation = [[[DDAnnotation alloc] initWithCoordinate:theCoordinate addressDictionary:nil] autorelease];
annotation.title = title;
annotation.subtitle = [NSString stringWithFormat:#"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
[MapView addAnnotation:annotation];
}
when I press centre button I am doing the following code and store last array in annotation.
-(IBAction)CenterPressed:(id)sender
{
//40.439631,-3.698273 -spain centre
[lastAnnotation addObjectsFromArray:MapView.annotations];
NSLog(#"last annotation array=%#",lastAnnotation);
for (id annotation in [MapView annotations])
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
continue;
}
[MapView removeAnnotation:annotation];
}
[self PinDropwithlatitude:SetLat longitude:SetLong
droptitle:NSLocalizedString(#"Title", nil)];
}
the log for the array is showing me the
last annotations you can see below::
last annotation array=(
"<+40.43963100,-3.69827300> +/- 0.00m",
"<+40.43923187,-3.68722200> +/- 0.00m",
"<+40.43792343,-3.67670774> +/- 0.00m",
"<+40.43772888,-3.66711617> +/- 0.00m"
)
2)UNDOButton: which removes the currently placed annotation and redrop the previous annotation, for that i have removed the annotaion from mapview, and redrop the annotation last annotation from array which i have maintained previously,using the code:
-(IBAction)undoPressed:(id)sender
{
if ([lastAnnotation count]>0)
{
int countAnn = [lastAnnotation count];
[MapView removeAnnotation:[lastAnnotation objectAtIndex:countAnn-1]];
//[MapView delete:[lastAnnotation objectAtIndex:countAnn-1]];
[lastAnnotation removeObjectAtIndex:countAnn-1];
double latitude = [[[lastAnnotation objectAtIndex:[lastAnnotation count]-1] annotation]coordinate].latitude;
double longitude = [[[lastAnnotation objectAtIndex:[lastAnnotation count]-1]annotation]coordinate].longitude;
NSLog(#"count = %d",[lastAnnotation count]);
[self PinDropwithlatitude:latitude longitude:longitude droptitle:NSLocalizedString(#"Title", nil)];
}
}
but when i press the undo button it crashes with the following error
-[DDAnnotation annotation]: unrecognized selector sent to instance 0x79b8f40
I don't get to know, where exactly the problem arises. can any one please help me to point out my mistake in the above code.
thanks
can you try
[lastAnnotation lastObject]
instead of
[[lastAnnotation objectAtIndex:[lastAnnotation count]-1]
like
[MapView removeAnnotation:[lastAnnotation lastObject]];
[lastAnnotation removeObject:[lastAnnotation lastObject]];

how to get user current location in simulator in iphone sdk?

I am getting user location from this method
/*Region and Zoom*/
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocation *location1 = [locationManager location];
CLLocationCoordinate2D location = [location1 coordinate];
location.latitude = location.latitude;
location.longitude = location.longitude;
region.span=span;
region.center = location;
/*Geocoder Stuff*/
MKReverseGeocoder *geoCoder=[[MKReverseGeocoder alloc] initWithCoordinate:location];
geoCoder.delegate = self;
[geoCoder start];
mapView.showsUserLocation = TRUE;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
This works perfectly but when I try to run this code in simulator then it gets crashed at the point of [mapView setRegion:region animated:TRUE]; this tells it doesn't get the latitude and longitude values. So how can I make this code run in simulator perfectly.
I have to make this code run in simulator, is it possible?
On iOS5 Simulator by default you it will show some location in USA. If you want to change that location, in iOS Simulator menu, go to Debug -> Location -> Custom Location.
There you can set the latitude and longitude and test the app accordingly. This works with mapkit and also with CLLocationManager.
try this path-
Go to product->Edit Scheme->Options->select Allow Location simulation
it won't be your exact location! But at least you can verify your code is working.

CoreLocation startMonitoringRegion not firing didenterregion /didexitregion delegates

i cant seem to get any of the didenterregion/didexitregion delegates firing , been stuck for 2 days on it
im using xcode4.2 with ios5 sdk and location simulation,
has anyone any luck getting this to work? is a 100m radius too small?(ive tried 1000 doesnt work too) or is there something wrong with my code
checked also with normal location updating and user location indeed enters region. could not find an answer anywhere as well
MainViewController(also CLLocationManagerDelegate)'s ViewDidLoad method:
mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeStandard;
mapView.delegate = self;
[self.view addSubview:mapView];
MKCoordinateRegion region;// = {{0.0,0.0},{0.0,0.0}};
MKCoordinateSpan span;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
CLLocationCoordinate2D coordinates =CLLocationCoordinate2DMake(1.34537, 103.84515);
region.span = span;
region.center = coordinates;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
if (locationManager==nil){
locationManager = [[CLLocationManager alloc] init];
}
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
self.locationManager.delegate =self;
if ([CLLocationManager regionMonitoringAvailable]){
NSLog(#"regionMonitoring available");
CLRegion* region3 = [[CLRegion alloc] initCircularRegionWithCenter:coordinates radius:100 identifier:#"region3"];
[self.locationManager startMonitoringForRegion:region3 desiredAccuracy:1.0];
NSLog(#"region = %#",region3);
NSLog(#"loc max = %f",self.locationManager.maximumRegionMonitoringDistance);
NSLog(#"location manger monitored regions = %#",self.locationManager.monitoredRegions);
also implemented all delegate methods neccessary
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didfailwitherror");
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"latitude %+.10f, longitude %+.10f\n",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
NSLog(#"didenterregion");
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
NSLog(#"didexitregion");
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{NSLog(#"monitoringDidFailForRegion");}
thanks for any help
How have you been testing the location? The regions are a little tricky, but once they are set up, they usually work great. What I usually do to test the region is just use the custom location in the Simulator. Use the same coordinates you are adding your region. 1000M is pretty large. I've tested with regions as small as 15-20M. If you have the right coordinates, it should be fine. Try switching back and forth from Apple HQ and your custom coordinates. You should be able to see the didEnter and didExit methods trip almost instantly if you have them logged out.
The one area you may have issues with is that the callbacks might not be getting to your method. None of my regions worked until I set up my AppDelegate as the CLLocationManagerDelegate. The app delegate finally got the updates and processed them correctly. I would try adding these callbacks there first, then work your way back to your mainviewcontroller. Hope this helps.
UPDATE
Since answering this question, I have learned a few things more about region monitoring. Regions get added with a minimum region size despite what you add for default. I was told by an Apple engineer that the minimum region size is 100M. I still feel they are very accurate, but it might help to understand why it will check you in. If you need precision better than 100M, you may need to look at other options.

Why didn't my map annotation move?

I am implementing a mapView whereby an annotation will be placed when the user search for an address. But somehow, the annotation sometime doesn't move and update to the new coordinate. It's only upon zooming the map, then will it update to the new location. The subtitle did get updated though.
- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
SVGeocoder *geocodeRequest = [[SVGeocoder alloc] initWithAddress:searchBar.text inRegion:#"sg"];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous];
}
- (void)geocoder:(SVGeocoder *)geocoder didFindPlacemark:(SVPlacemark *)placemark {
if (annotation) {
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = [NSString
stringWithFormat:#"%#", placemark.formattedAddress];
}
else {
annotation = [[MyAnnotation alloc]
initWithCoordinate:placemark.coordinate
title:#"Tap arrow to use address"
subtitle:[NSString
stringWithFormat:#"%#", placemark.formattedAddress]];
[mapView addAnnotation:annotation];
}
MKCoordinateSpan span;
span.latitudeDelta = .001;
span.longitudeDelta = .001;
MKCoordinateRegion region;
region.center = placemark.coordinate;
region.span = span;
[mapView setRegion:region animated:TRUE];
[searchBar resignFirstResponder];
}
I don't think MKMapView gets notified of changes to an annotation's location. The documentation of MKAnnotation's setCoordinate: says: "Annotations that support dragging should implement this method to update the position of the annotation." so it seems that's the only purpose of this method is for supporting dragging of pins.
Try removing the annotation from the map view before changing its coordinates and then add it back to the map view.
Nothing in your code (that you've shown) tells mapView that the annotation's location has changed. The annotation itself probably can't do it in -moveAnnotation because annotations generally don't know what map or maps they've been added to (nor should they).
The right way to move an annotation is to to remove it from the MKMapView that's using it, update its location, and then add it back to the map. You can't just change the annotation's location after it has been added to the map because the map may very well cache the location or sort the annotation according to its location, and there's no method in MKMapView to tell the map that the location has changed.
I'd change your conditional to something like this:
if (annotation == nil) {
annotation = [[MyAnnotation alloc] init];
annotation.title = #"Tap arrow to use address";
}
[mapView removeAnnotation:annotation];
[annotation moveAnnotation:placemark.coordinate];
annotation.subtitle = placemark.formattedAddress;
[mapView addAnnotation:annotation];
This assumes that it's safe to call -init in place of -initWithCoordinate:title:subtitle:; if not, you'll want to change that.