MkMapView Geofence CreationThis is basic requirement to have Geofence around a Location point on MKMapView. we may have Circular, square and Polygon type of geofence. By having that geofence any annotation(Marker) on MKMapView can be idenitifed as it is residing in that geofence or not , basis on that we can have our customized action. Before this you must have knowledge of rendering MKMapView and pointing Annotation on it (if no then please visit previous blogs for this Knowledge) here.
Some Assumption for below code
1) mapView is MKMapView object
2) overlay is “id” Object that consisting current geofence object on mapView
Add Circular Geofence
-(void)addCircle:(CLLocation *)center radius:(float)radius{
// Overlay is id object that consisting your geofence object so before assigning new Geofence object to it wa are going to
remove already assigned geofence to it from our MkMapView object mapView
// mapView is our MkMapView global object in .h file
if (overLay != nil) {
[mapView removeOverlay:overLay];
overLay = nil;
[overLay release];
}
MKCircle *circleOverLay = [[MKCircle circleWithCenterCoordinate:center.coordinate radius:radius] autorelease];
overLay = circleOverLay;
[overLay retain];
[mapView addOverlay:circleOverLay];
}
Circular Geofence
Add Square Geofence
(MKMapRect) mapRectForCoordinateRegion:(MKCoordinateRegion)coordinateRegion {
CLLocationCoordinate2D topLeftCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude
+ (coordinateRegion.span.latitudeDelta / 2.0), coordinateRegion.center.longitude
- (coordinateRegion.span.longitudeDelta / 2.0));
MKMapPoint topLeftMapPoint = MKMapPointForCoordinate(topLeftCoordinate);
CLLocationCoordinate2D bottomRightCoordinate = CLLocationCoordinate2DMake(coordinateRegion.center.latitude
- (coordinateRegion.span.latitudeDelta / 2.0), coordinateRegion.center.longitude
+ (coordinateRegion.span.longitudeDelta / 2.0));
MKMapPoint bottomRightMapPoint = MKMapPointForCoordinate(bottomRightCoordinate);
MKMapRect mapRect = MKMapRectMake(topLeftMapPoint.x, topLeftMapPoint.y, fabs(bottomRightMapPoint.x - topLeftMapPoint.x),
fabs(bottomRightMapPoint.y - topLeftMapPoint.y));
return mapRect;
}
-(void)addSquarePolygon:(CLLocation*)center radius:(float)radius{
if (overLay != nil) {
[mapView removeOverlay:overLay];
overLay = nil;
[overLay release];
}
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center.coordinate, radius*2, radius*2);
MKMapRect squareRect = [self mapRectForCoordinateRegion:region];
MKMapPoint leftTop = MKMapPointMake(MKMapRectGetMinX(squareRect), MKMapRectGetMinY(squareRect));
MKMapPoint rightTop = MKMapPointMake(MKMapRectGetMaxX(squareRect), MKMapRectGetMinY(squareRect));
MKMapPoint leftBottom = MKMapPointMake(MKMapRectGetMinX(squareRect), MKMapRectGetMaxY(squareRect));
MKMapPoint rightBottom = MKMapPointMake(MKMapRectGetMaxX(squareRect), MKMapRectGetMaxY(squareRect));
MKMapPoint squarePoints[] = {leftTop,rightTop,rightBottom,leftBottom};
MKPolygon *squareOverlay = [[MKPolygon polygonWithPoints:squarePoints count:4] autorelease];
overLay = squareOverlay;
[overLay retain];
[mapView addOverlay:squareOverlay];
}
Square Geofence
Add Polygon Geofence
-(void)addPolygon:(CLLocation*)center radius:(float)radius
{
if (overLay != nil)
{
[mapView removeOverlay:overLay];
overLay = nil;
[overLay release];
}
MKPolygon *polygonOverlay = [[MKPolygon polygonWithPoints:self.mapPoints count:mapPointsCount] autorelease];
//here mapPoints is an array that consisting MKMapView Coordingates , that will be used to generate polygon on MkMapView
overLay = polygonOverlay;
[overLay retain];
[mapView addOverlay:polygonOverlay];
}
// [mapView addOverlay:circleOverLay] calls -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)
overlay Delegate that is defined below
Polygon Geofence
How to Use
-(void)generateOverlay{
CLLocation *jaipurLocation = [[[CLLocation alloc]initWithLatitude:26.940435 longitude:75.783691]autorelease];
[self addCircle:jaipurLocation radius:1000];
[self addSquarePolygon:jaipurLocation radius: 1000];
// Radius is passed in meter
}
0 comments:
Post a Comment