For some reason, when I use [mapView addOverlay:], nothing happens.
Code:
NSData* dataLat = [[NSUserDefaults standardUserDefaults] objectForKey:#"latKey"];
NSArray* overlayLat = [NSKeyedUnarchiver unarchiveObjectWithData:dataLat];
double lats[overlayLat.count];
NSData* dataLong = [[NSUserDefaults standardUserDefaults] objectForKey:#"longKey"];
NSArray* overlayLong = [NSKeyedUnarchiver unarchiveObjectWithData:dataLong];
double longs[overlayLong.count];
for(int iii = 0; iii < overlayLat.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLat objectAtIndex:iii];
lats[iii] = [a doubleValue];
}
for(int iii = 0; iii < overlayLong.count; iii++)
{
NSNumber* a = (NSNumber*)[overlayLong objectAtIndex:iii];
longs[iii] = [a doubleValue];
}
int size = (sizeof(lats) / sizeof(lats[0]));
NSLog(#"%d", size);
MKMapPoint points[size];
for(int iii = 0; iii < overlayLong.count; iii++)
{
MKMapPoint point = MKMapPointMake(lats[iii], longs[iii]);
if(lats[iii] != 0)
points[iii] = point;
}
for(int iii = 0; iii < 15; iii++)
{
NSLog(#"Lat (x):%f", points[iii].x);
NSLog(#"Long (y):%f", points[iii].y);
}
MKPolyline* line = [MKPolyline polylineWithPoints:points count:size];
[mapView addOverlay:line];
NSLog(#"finished");
The vast majority of this code just accesses the data and turns it into usable coordinates. My question is (because I know that the coordinates are valid because of the NSLogs), why doesn't anything get added to the mapView when addOverlay: is called? I can post more code if need be. Thanks!
The polylineWithPoints:count: method takes a C array of MKMapPoint structs.
An MKMapPoint is not the same as a CLLocationCoordinate2D (latitude, longitude). You are creating MKMapPoint structs using MKMapPointMake but giving it latitude and longitude values. Such an MKMapPoint is not at the expected location.
Either use MKMapPointForCoordinate to create an MKMapPoint from lat/long coordinates or, probably easier, create a C array of CLLocationCoordinate2D structs and call polylineWithCoordinates:count: instead.
Could it be your 'size' is wrong? Your points array will also have missing items anywhere the source data had 0. I'd go with an NSMutableArray and add items to that if they are deemed valid, then count that at the end and pass it to polylineWithPoints.
Related
Im looking for a way to detect if the user is in The Netherlands or in Belgium. I don't want to call an API or use the users telephone services, so everything needs to be done locally and should be something easy and quick, small check. I have the users location each second and want to check each minute.
Goal:
The user is travelling with our app and I want to show a different view when he/she arrived Belgium/Netherlands.
I've found two shape files from http://www.diva-gis.org/datadown with Netherlands and Belgium. These files are quite small and now Im looking for a way to create this method:
- (BOOL)isUserInNetherlands:(CLLocation)location;
which should check the location is it is in the content of the shapefile.
The shapefile for the Netherlands looks something like this:
I can convert the shapefile to a sqlLite database but not idea what to do next.
Any idea's?
One way to do this is:
1) convert the shapefile to geoJson
2) load the geoJson file data in your code.
3) create polygons from these gps points (note: CountryDetectorController is my controller class)
+ (MKPolygon *)overlaysFromPolygons:(NSArray *)polygons title:(NSString *)title;
{
NSMutableArray *interiorPolygons = [NSMutableArray arrayWithCapacity:[polygons count] - 1];
for (int i = 1; i < [polygons count]; i++) {
[interiorPolygons addObject:[CountryDetectorController polygonFromPoints:polygons[i] interiorPolygons:nil]];
}
MKPolygon *overlayPolygon = [CountryDetectorController polygonFromPoints:polygons[0] interiorPolygons:interiorPolygons];
overlayPolygon.title = title;
return overlayPolygon;
}
+ (MKPolygon *)polygonFromPoints:(NSArray *)points interiorPolygons:(NSArray *)polygons;
{
NSInteger numberOfCoordinates = [points count];
CLLocationCoordinate2D *polygonPoints = malloc(numberOfCoordinates * sizeof(CLLocationCoordinate2D));
NSInteger index = 0;
for (NSArray *pointArray in points) {
polygonPoints[index] = CLLocationCoordinate2DMake([pointArray[1] floatValue], [pointArray[0] floatValue]);
index++;
}
MKPolygon *polygon;
if (polygons) {
polygon = [MKPolygon polygonWithCoordinates:polygonPoints count:numberOfCoordinates interiorPolygons:polygons];
} else {
polygon = [MKPolygon polygonWithCoordinates:polygonPoints count:numberOfCoordinates];
}
free(polygonPoints);
return polygon;
}
4) create a CGPath from the polygon
+ (CGPathRef)pathForPolygon:(MKPolygon*)aPolygon;
{
CGMutablePathRef mpr = CGPathCreateMutable();
MKMapPoint *polygonPoints = aPolygon.points;
size_t nCount = aPolygon.pointCount;
for (int p = 0; p < nCount; p++)
{
MKMapPoint mp = polygonPoints[p];
if (p == 0)
CGPathMoveToPoint(mpr, NULL, mp.x, mp.y);
else
CGPathAddLineToPoint(mpr, NULL, mp.x, mp.y);
}
return mpr; //Keep in memory;
}
5) check if coordinate is in CGPath (pathsBelgium is an array with NSValue of CGPaths)
- (void)checkCoordinate:(CLLocationCoordinate2D)coordinate;
{
MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
CGPoint mapPointAsCGP = CGPointMake(mapPoint.x, mapPoint.y);
BOOL userIsInBelgium = FALSE;
for(NSValue *valuePathBE in pathsBelgium)
{
CGPathRef pathBe;
[valuePathBE getValue:&pathBe];
BOOL pointInBelgium = CGPathContainsPoint(pathBe, NULL, mapPointAsCGP, FALSE);
if(pointInBelgium)
{
userIsInBelgium = TRUE;
break;
}
}
NSLog(#"User is in Belgium: %i", userIsInBelgium);
}
A simpler method would be to get the user's location and then do a reverse geocode. Then you can extract the country from Apple's API instead of doing all the work yourself.
More here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html
I have this situation where my app crashes when I zoom out the map.
The problem arises because of the large number of annotations that I'm adding. Please have a look at my code below :
- (void) plotUsersInMap
{
for (id<MKAnnotation> annotation in self.mapView.annotations) {
[self.mapView removeAnnotation:annotation];
}
NSUInteger count = //get total count
NSLog(#"count * %d", count);
for (int i = 0; i < count; i++)
{
NSNumber *latitude = //get latitude from json
NSNumber *longitude = //get longitude from json
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude.doubleValue;
coordinate.longitude = longitude.doubleValue;
#autoreleasepool {
MyLocation *annotation = [[MyLocation alloc] initWithName:#"test" coordinate:coordinate QuestionId:nil];
//annotations are added
[self.mapView addAnnotation:annotation];
}
}
}
Here I'm trying to add more than 400 pins which I think is the cause of crash [probably a memory leak!]. I would like to know if there is any way to add the pins one by one as I zoom out?
Map in initial stage, without any problem :
And when I zoom out :
Try clustering. Basically you group together annotations.
The code repo from the article I linked to: https://github.com/applidium/ADClusterMapView
I have an array of coordinates which represents a users route. I want to be able to redraw this route and have another user follow it exactly. Although I only want to save significant coordinates (waypoints) when the route will change direction.
Is there a way I can convert the array of coordinates to a bezier curve and use this so I only get significant coordinates instead of saving every one in an array(most of which are in the same direction and aren't needed)
I want to be able to reconstruct the route to give waypoints. These waypoints will be used to direct the user along the route as eventually I will be using MKDirections to direct them to each waypoint.
You should not use MKDirections since it is not made for following a precalculated path but to give you the fastest route to the destination! Therefore I will use MKMapOverlayRenderer instead!
There are several things you can to achieve this. I want to talk about easy algorithm for removing the unnecessary waypoints:
NSArray* path = YOURPATH
NSMutableArray* waypoints = [path mutableCopy];
CGFloat smoothness = 0.01;
for(NSInteger scale=1;scale<log2(waypoints.count);scale++)
{
for(NSInteger index = 0; index<waypoints.count-2;index++)
{
CLLocation *start = [waypoints objectAtIndex:index];
CLLocation *middle = [waypoints objectAtIndex:index+1];
CLLocation *end = [waypoints objectAtIndex:index+2];
CGFloat dist_start_end = [end distanceFromLocation:start];
CGFloat dist_start_middle_end = [middle distanceFromLocation:start]+[end distanceFromLocation:middle];
CGFloat diff = dist_start_end-dist_start_middle_end;
CGFloat ratio = diff/(dist_start_end);
if (ratio<1+smoothness) {
[waypoints removeObjectAtIndex:index+1];
index-=1;
}
}
}
This should remove the unwanted points. You should playaround with the 'smoothness' variable. The bigger the value, the more details will be removed! (therefore better compression)
Now with this array, you can construct your bezier path!
I will not go into the details on how to actually put the bezier path onto the map, there are so many other questions about this on Stackoverflow and also tutorials on the internet. But I will shortly touch on how you should create/use the two control points of the bezier path:
Given the waypoints array we created above, you will probably add some additional control points, in order to avoid driving directions taking a shortcut through a building. Therefore you should calculate the control points between two actual points on the path like this:
CGFloat cornerRadius = 5;
NSMutableArray* pathPoints = [NSMutableArray new];
if (waypoints.count>1) {
[pathPoints addObject:[waypoints objectAtIndex:0]];
[pathPoints addObject:[waypoints objectAtIndex:0]];
[pathPoints addObject:[waypoints objectAtIndex:1]];
[pathPoints addObject:[waypoints objectAtIndex:1]];
}
for (NSInteger index = 1;index<waypoints.count-1;index++) {
CLLocation* lastLocation = [waypoints objectAtIndex:index-1];
CLLocation* currentLocation = [waypoints objectAtIndex:index];
CLLocation* nextLocation = [waypoints objectAtIndex:index+1];
[pathPoints addObject:currentLocation];
CGFloat latDiff = currentLocation.coordinate.latitude - lastLocation.coordinate.latitude;
CGFloat longDiff = currentLocation.coordinate.longitude - lastLocation.coordinate.longitude;
CGFloat dist = [currentLocation distanceFromLocation:lastLocation];
CGFloat controlPointALat = cornerRadius*latDiff/dist;
CGFloat controlPointALong = cornerRadius*longDiff/dist;
CLLocation* controlPointA =
[[CLLocation alloc] initWithLatitude:controlPointALat longitude:controlPointALong];
[pathPoints addObject:controlPointA];
if (index<waypoints.count-2) {
CLLocation* nextNextLocation = [waypoints objectAtIndex:index+2];
latDiff = nextNextLocation.coordinate.latitude - nextLocation.coordinate.latitude;
longDiff = nextNextLocation.coordinate.longitude - nextLocation.coordinate.longitude;
dist = [nextNextLocation distanceFromLocation:nextLocation];
CGFloat controlPointBLat = cornerRadius*latDiff/dist;
CGFloat controlPointBLong = cornerRadius*longDiff/dist;
CLLocation* controlPointB =
[[CLLocation alloc] initWithLatitude:controlPointBLat longitude:controlPointBLong];
[pathPoints addObject:controlPointB];
}else{
[pathPoints addObject:controlPointA];
}
[pathPoints addObject:nextLocation];
}
Now you can use this pathArray to build your bezierPaths with a simple for loop:
for(NSInteger index=0;index<pathPoints.count-3;index+=4)
{
CLLocation *start = [pathPoints objectAtIndex:index];
CLLocation *controlPointA = [pathPoints objectAtIndex:index+1];
CLLocation *controlPointB = [pathPoints objectAtIndex:index+2];
CLLocation *end = [pathPoints objectAtIndex:index+3];
//BEZIER CURVE HERE
}
You can either use a UIBezierPath and use the CGPath property to create your MKMapOverlayRenderer, or you can directly use a CGPath. Later adding the path to your map is described here:
MKMapOverlayRenderer
Createing a UIBezierPath is described here:
UIBezierPath
You can take an array of NSValues because you can't add a CGPoint directly into an Array
This is how we do it
NSMutableArray *arrayOfPoints = [[NSMutableArray alloc] init];
[arrayOfPoints addObject:[NSValue valueWithCGPoint:CGPointMake(10, 20)]];
// here point is the cgpoint you want to add
Now you have your points added to an array what you can do is traverse every object and add them to your UIBezierPath
NSInteger i = 0;
UIBezierPath _path1 = [UIBezierPath bezierPath];
for(NSValue *val in arrayOfPoints){
if (i==0){
[_path1 moveToPoint:[val CGPointValue]];
}
else{
[_path1 addLineToPoint:[val CGPointValue]];
}
i++;
}
I hope this helps and solve your problem
I have an array of waypoints stored which have latitude and longitude values. I have created a for loop to loop through the array and compare my current CLLocation positions latitude and longitude to find which of the waypoints i am closest to. I also need to get the second closest waypoint and store this ac s a CLLocation object as well but cannot get it working.
The logic would be something like this
am I closest
yes
move closest location to second closest
set as second closest
loop again to get the closest point
My code:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//set latestlocation as the last object from the locations array
CLLocation *currentLocation = [locations lastObject];
//declare a closestpoint object
CLLocation *closestWayPointToCurrentLocation;
//declare a second closest point object
CLLocation *secondClosestWayPointToCurrentLocation;
//set the distance to a high number
float distance = 10000000;
float secondClosestWaypointDistance = 10000000;
//load in plist
NSString *plistName = [self.mapsInfo objectForKey:#"plistName"];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Chester" ofType:#"plist"];
NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:#"plist"];
//store in array called waypoints
NSArray *waypoints= [NSArray arrayWithContentsOfFile:path];
//declare a variable for locationNum (the waypoints)
int locationNum = 0;
for (NSDictionary *point in waypoints) {
CLLocation *waypointLocation = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[point objectForKey:#"Lat"]floatValue] longitude:[(NSNumber *)[point objectForKey:#"Long"]floatValue]];
float waypointDistanceFromCurrentLocation = [currentLocation distanceFromLocation:waypointLocation];
//secondClosestWayPointToCurrentLocation = waypointLocation;
if(waypointDistanceFromCurrentLocation < distance) {
//todo: move the current closestWayPointToCurrentLocation into second postion
//update the second closest waypoint distance variable also with distance
distance = waypointDistanceFromCurrentLocation;
closestWayPointToCurrentLocation = waypointLocation;
if(closestWayPointToCurrentLocation == waypointLocation) {
}
}
else
{
//check against the second position
//if closer than second position, replace it with new waypoint with code similar to above
}
If you are sure the point you is always the penultimate then you can retrive it like this
int totalNumberOfWaypoints = [waypoints count];
//Get penultimate waypoints
NSDictionary *penultimateWaypoint = [waypoints objectAtIndex:(totalNumberOfWaypoints - 2)];
Why dont you just make a sorted array of points, sorted by distance in ascending order?
NSMutableArray *waypoints= [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *point in waypoints) {
[waypoints sortUsingComparator: (NSComparisonResult) ^ (id obj1, id obj2) {
CLLocation *waypointLocation1 = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[obj1 objectForKey:#"Lat"]floatValue] longitude:[(NSNumber *)[obj1 objectForKey:#"Long"]floatValue]];
CLLocation *waypointLocation2 = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[obj2 objectForKey:#"Lat"]floatValue] longitude:[(NSNumber *)[obj2 objectForKey:#"Long"]floatValue]];
float distance1 = [currentLocation distanceFromLocation:waypointLocation1];
float distance2 = [currentLocation distanceFromLocation:waypointLocation2];
if (distance1 < distance2) {
return NSOrderedAscending;
}
else if (distance1 > distance2) {
return NSOrderedDescending
}
else
return NSOrderedSame;
}];
This way you can always have the closest point at [waypoints objectAtIndex:0] and second closest point at [waypoints objectAtIndex:1] and so on.
less work, better result IMHO
I have a database that contains 16000 annotations. When the user is zooming out, there are too many points loaded and points overlap. I found a filter with code but i want to load some point only from SQL, not to load all and filter it after. I don't know if there is a stored procedure in iOS or other solution.
This is my code
iphoneScaleFactorLatitude = 3.3;
float iphoneScaleFactorLongitude = 6.38;
float latDelta = self.mapView.region.span.latitudeDelta/iphoneScaleFactorLatitude;
float longDelta = self.mapView.region.span.longitudeDelta/iphoneScaleFactorLongitude;
NSMutableArray *shopsToShow = [[NSMutableArray alloc] initWithCapacity:0];
for (int i=0; i<[placesToFilter count]; i++) {
MKPointAnnotation *checkingLocation = [placesToFilter objectAtIndex:i];
CLLocationDegrees latitude = [checkingLocation coordinate].latitude;
CLLocationDegrees longitude = [checkingLocation coordinate].longitude;
bool found=FALSE;
for (MKPointAnnotation *tempPlacemark in shopsToShow) {
if(fabs([tempPlacemark coordinate].latitude-latitude) < latDelta &&
fabs([tempPlacemark coordinate].longitude-longitude) <longDelta ){
[self.mapView removeAnnotation:checkingLocation];
found = TRUE;
break;
}
}
if (!found){
[shopsToShow addObject:checkingLocation];
[self.mapView addAnnotation:checkingLocation];
}
}
But they load too many point and my problem is that I don't want to load all the points because each chagement I have to use the algorithm to the sorting.