I have NSMutableArray:
userCoordinates = [[d objectForKey:#"geo"] objectForKey:#"coordinates"];
NSLog(#"%#",userCoordinates);
NSLog shows:
(
"19.365367",
"-99.159887"
)
Then I need to convert this array to CllocationCoordinate2D to use it for create annotation.
Sorry my english. Thanks.
It seems that your array contains two NSString objects - to convert them to the appropriate floating-point numbers, use the doubleValue method of NSString:
double lat = [(NSString *)[userCoordinates objectAtIndex:0] doubleValue];
double lon = [(NSString *)[userCoordinates objectAtIndex:1] doubleValue];
CLLocationCoordinate2D coords = (CLLocationCoordinate2D){ lat, lon };
Related
I have an app that uses MKMapView. In my app, I have declared an array that will hold the response from the API. The data from the API are the jobs to be pinned in the map (clustered annotations). The array that holds the jobs from the API will/needs to be filtered by the jobs pins that are visible in the map. I am able to filter the coordinates (visible or not visible in the map) but I am having troubles on storing the data (coordinates that are visible) in a new array.
Here's what I have so far:
In regionDidChangeAnimated in my mapview
[ar objectAtIndex:0];
NSMutableDictionary *visibleJobs;
for(NSDictionary *loc in ar)
{
CLLocationDegrees Lat = [[[loc objectForKey:#"sub_slots"] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longTitude = [[[loc objectForKey:#"sub_slots"] objectForKey:#"longitude"] doubleValue];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);
MKMapPoint mkPoint = MKMapPointForCoordinate(point);
BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
if(contains)
{
NSLog(#"Contains:1");
}
else
{
NSLog(#"Contains:0");
}
}
Any help will be very much appreciated.
Thanks!
why don't you use NSMutableArray
try this
NSMutableArray *visibleJobs = [[NSMutableArray alloc]init];
for(NSDictionary *loc in ar)
{
CLLocationDegrees Lat = [[[loc objectForKey:#"sub_slots"] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longTitude = [[[loc objectForKey:#"sub_slots"] objectForKey:#"longitude"] doubleValue];
CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);
MKMapPoint mkPoint = MKMapPointForCoordinate(point);
BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
if(contains)
{
NSLog(#"Contains:1");
[visibleJobs addObject:loc];
}
else
{
NSLog(#"Contains:0");
}
}
Now visible jobs array contains dictionary of all pin data which in currently visible on map
In my application I have longitude and latitude of a location on my server. When I retrieve the lat and lon from the server I save them on string.
The problem is I can not use strings in GMSCamerPosition:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latit
longitude:longt
zoom:10];
so I need to convert them.
I tried to convert them to integer but that could not help. Also I tried to convert them to float/ double but that also did not help because it take the number and 6 digits only after .
for example
if this the string of latitude = 25.8837788378837
converting it to float/double produces 25.883778
Here I'm retrieving the data
NSURL *ur = [NSURL URLWithString:[NSString stringWithFormat:#"......"]];
NSData *data = [NSData dataWithContentsOfURL:ur];
json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for(int i=0; i<json.count; i++)
{
Lng = [[json objectAtIndex:i] objectForKey:#"Lng"];
Lat = [[json objectAtIndex:i] objectForKey:#"Lat"];
}
the Lng and Lat are my strings.
Lat = 25.431609385185958
Lng = 49.583187103271484
in my case.
And Here i use them
- (void)viewDidLoad
{
double latit = [lat doubleValue];
double longt = [lon doubleValue];
NSLog(#"%f",latit);
[super viewDidLoad];
// Do any additional setup after loading the view.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latit
longitude:longt
zoom:10];
double could not make it also. i get only this = 25.431609.
i know that i have to change NSLog to %.13f to print it all. but i did not want to print it i want to use the variable in GMSCameraPosition.
Everything is working fine as rmaddy said you're not printing all digits. Try this:
NSString *lat = #"25.8837788378837";
double doubleLat = [lat doubleValue];
NSLog(#"%.13f",doubleLat);
NSString *lat = #"25.8837788378837";
NSLog(#"%.13f", [lat doubleValue]);
I want to subtract two latitudes from each other to find the shortest distance, but I get this error, "Arithmetic on pointer to interface 'NSNumber', which is not a constant size in non-fragile ABI" If I change the - to a + I get a different error "Invalid operands to binary expression ('NSNumber *' and 'NSNumber *')" I've tried using doubles and many combinations of things, but it just doesn't work.
NSNumber *userLatitude = [NSNumber numberWithDouble:43.55];//sample
NSArray *listOfCities = [managedObjectContext executeFetchRequest:request error:&error];
for (CityList *item in listOfCities){
NSLog(#"latitude is %#",item.latitude);
NSNumber *distanceLat = userLatitude - item.latitude;
I will then insert them into a mutable array along with the longitudes and compare the distance. One possible solution using CLLocation would be
double distance = [usersCurrentLoc distanceFromLocation:otherLoc];
where usersCurrentLoc and otherLoc are both CLLocation variables.
I also want use the latitude and longitudes individually so I can do some custom plotting, and they are also stored separately, so I'd like to figure out the correct data types and most efficient solution.
item.latitude comes from core-data with the data model type of double and X-code auto generated the CityList class with a property of NSNumber * latitude;
If you want subtract two NSNumbers, then use this
NSNumber *distanceLat = [NSNumber numberWithFloat:([userLatitude floatValue] - [item.latitude floatValue])];
This:
NSNumber *distanceLat = userLatitude - item.latitude;
needs to be:
NSNumber *distanceLat = #([userLatitude doubleValue] - item.latitude);
If item.latitude is also an NSNumber then you need to call doubleValue on it too.
NSNumber is an object. You can't do math on the object. You need to use doubleValue to get its value and then you need to wrap the result in a new NSNumber instance.
BTW - why bother with NSNumber here? Why not do:
double userLatitude = 43.55;
double distanceLat = userLatitude - item.latitude;
CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];
CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
The above one can be used to find distance between two locations
I have two strings which hold values say for ex:35.5044752 97.3955550
Let me convert it :
double f1=[la doubleValue];
double f2=[lo doubleValue];
(value of f1 and f2 is dynamic say for example f1= "35.5044752" f2="97.3955550" )
if i want to print it in NSLog i will do as follows :
NSLog(#" %f %f ",f1,f2);
And it returns 35.504475 97.395555
hence i change it as
NSLog(#" %0.7f %0.7f ",f1,f2);
And gets the full values like 35.5044752 97.3955550
Now i need it to use in the Coordinate like below:
annotation.coordinate=CLLocationCoordinate2DMake(coord.longitude, coord.longitude);
My Question is how can i implement %0.7f here like which i made in NSlog ?
so that i should take input fully instead of reducing or altering the value.
make a try like this. Directly pass values to obj center
CLLocationCoordinate2D center;
...
else if ([elementName isEqualToString:#"Lat"]) {
center.latitude = [[attributeDict objectForKey:#"degrees"] doubleValue];
}
else if ([elementName isEqualToString:#"Lon"]) {
center.longitude = [[attributeDict objectForKey:#"degrees"] doubleValue];
}
...
OR
Archived the coordinate in foundLocation:
NSNumber *latitudeObject = [NSNumber numberWithDouble:coord.latitude];
NSNumber *longitudeObject = [NSNumber numberWithDouble:coord.longitude];
NSArray *coordinateArray = [NSArray arrayWithObjects:latitudeObject, longitudeObject, nil];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:coordinateArray]
forKey:WhereamiCoordinatePrefKey];
Unarchived the coordinate in viewDidLoad:
NSArray *coordinateArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]
objectForKey:WhereamiCoordinatePrefKey]];
CLLocationCoordinate2D savedCoordinate = CLLocationCoordinate2DMake([[coordinateArray objectAtIndex:0] doubleValue],
[[coordinateArray objectAtIndex:1] doubleValue]);
MKCoordinateRegion savedRegion = MKCoordinateRegionMakeWithDistance(savedCoordinate, 250, 250);
[worldView setRegion:savedRegion animated:YES];
The %0.7f format specifier deals with how your value is displayed, not with how it is stored. A double is always double and has its inherent precision and nothing you can do, short of casting it to another data type, will change that.
As far as I know, the double data type offers the highest floating point precision of the standard data types. If you need greater precision than that, you're going to have to use something other than a double.
In other words, when you perform an operation on a double, it is always calculated to the full precision allowed by the double data type.
For more information on the subject, see the Wikipedia entry on floating point data types.
The topic says everything. why i get this error message at this 2 lines?
NSArray *coordinates = [locationDetails[#"coordinates"] componentsSeparatedByString:#","];
CLLocationDegrees *lat = [coordinates[1] doubleValue]; //here is the red arrow <----
and exactly this message will appear:
Initializing 'CLLocationDegrees *'(aka 'double *') with an expression
of incompatible type 'double'
Change this:
CLLocationDegrees *lat = [coordinates[1] doubleValue];
to:
CLLocationDegrees lat = [coordinates[1] doubleValue];
Get rid of the asterisk. CLLocationDegrees is not a class, it is a typedef for double (a basic type).