Circle Overlay is not showing on map - ios

I am trying to add a circle overlay to a map, but it is not appearing:
- (void) displayOverlayOnMap:(double) lat andlng: (double) lng
{
CLLocationCoordinate2D bostonCoord = CLLocationCoordinate2DMake(lat,lng);
//add MKCircle overlay...
MKCircle *circle = [MKCircle circleWithCenterCoordinate:bostonCoord radius:1000];
[self.mapView addOverlay:circle];
}
Any body have a clue why it's not showing?

here is an example.. you can also find one using polygons on apple's sites: https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW15
or you can use this example
Create overlay from user interaction on MKMapView?
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
//add pin where user touched down...
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = #"Hello";
[mapView addAnnotation:pa];
[pa release];
//add circle with 5km radius where user touched down...
MKCircle *circle = [MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:5000];
[mapView addOverlay:circle];
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
circleView.fillColor = [UIColor redColor];
return circleView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = #"Annotation";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = YES;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}

Use the code
CLLocationDistance radiusInMeters = 1000;
MKCircle *circle = [MKCircle circleWithCenterCoordinate:bostonCoord radius:radiusInMeters];
Instead of
MKCircle *circle = [MKCircle circleWithCenterCoordinate:bostonCoord radius:1000];
It worked for me.

Related

How to change MKCircle radius on mapview with animation?

I want to change the circle radius with animation like this:
https://www.youtube.com/watch?v=j0s5DUnEy3k
Here's my code:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
if (locations != nil) {
userLocation = locations.firstObject;
CLLocationDegrees lat = 0.05;
CLLocationDegrees lon = 0.05;
MKCoordinateSpan span = MKCoordinateSpanMake(lat, lon);
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(userLocation.coordinate.latitude, userLocation.coordinate.longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(location, span);
[self.myMapView setRegion:region animated:true];
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[self.myMapView addOverlay:circle];
}
}
I create user's location annotation and add a circle at user's location center
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.3];
circleView.lineWidth = 0.2;
return circleView;
}
and customized the circle at rendererForOverlay function
So, how can I change the circle radius with animation?
Anyone have idea to do this?

Prevent MKPolygon to have knots

I'm developing an app with a map in which the user can draw a polygon area.
My issue is what it's possible drawing polygons with knots (see the image) (I don't know if knot is the right word). I didn't find a simply way preventing the polygon to get knots.
For the case of the attached image, I would like the small curl to be removed and even the outline to be smoothed
Do you know a way to make that?
The process of drawing the polygon while the user is touching the screen, does use MKPolyline, MKPolygon and MKOverlay as follows:
- (void)touchesBegan:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesMoved:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
}
- (void)touchesEnded:(UITouch*)touch
{
CGPoint location = [touch locationInView:self.mapView];
CLLocationCoordinate2D coordinate = [self.mapView convertPoint:location toCoordinateFromView:self.mapView];
[self.coordinates addObject:[NSValue valueWithMKCoordinate:coordinate]];
[self didTouchUpInsideDrawButton:nil];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayPathView *overlayPathView;
if ([overlay isKindOfClass:[MKPolygon class]])
{
// create a polygonView using polygon_overlay object
overlayPathView = [[MKPolygonView alloc] initWithPolygon:overlay];
overlayPathView.fillColor = [UIColor redColor];
overlayPathView.lineWidth = 1.5;
return overlayPathView;
}
else if ([overlay isKindOfClass:[MKPolyline class]])
{
overlayPathView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
overlayPathView.fillColor = [UIColor redColor];
overlayPathView.lineWidth = 3;
return overlayPathView;
}
return nil;
}
MKOverlayPathView was deprecated since iOS 7.0. You'd use MKOverlayRenderer instead of it and also related map delegate method.
Try to play with miterLimit property of MKOverlayRenderer.
Example:
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonRenderer *polygonRenederer = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
polygonRenederer.fillColor = [UIColor redColor];
polygonRenederer.lineWidth = 1.5;
polygonRenederer.miterLimit = 10;
return polygonRenederer;
} else if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineRenderer *lineRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
lineRenderer.strokeColor = [UIColor redColor];
lineRenderer.lineWidth = 3;
return lineRenderer;
}
return nil;
}

MKPinAnnotationView not display properly

The annotationView bubble size is too small 9 out of 10 times.
I call [self.mapView selectAnnotation:self.selectedVenue animated:YES]; to show annotation view when select a row. Whether I set animated: YES or NO it still displays wrong size. However, if I increase size of map view height to at lease 200 pixels, everything looks fine except for the map view is too big for 3.5" screen.
I want the map view to be this size and annotation bubble to cover the title and subtitle correctly.
image: https://dl.dropboxusercontent.com/u/5105730/anno.png
Here's how I create my annotation view.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if ([annotation isKindOfClass:[FSVenue class]]) {
static NSString *PlaceAnnotationIdentifier = #"Place Identifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:PlaceAnnotationIdentifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PlaceAnnotationIdentifier];
}
annotationView.annotation = annotation;
UIButton *calloutButton = [UIButton buttonWithType:(UIButtonTypeContactAdd)];
calloutButton.tintColor = self.themeColor;
annotationView.enabled = YES;
annotationView.pinColor = MKPinAnnotationColorGreen;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = calloutButton;
annotationView.animatesDrop = YES;
return annotationView;
}
return nil;
}
You have to zoom the visible rect of your mapview after adding your annotations like this.
[self.yourMapview addAnnotations:self.yourAnnotationsArray];
[self zoomToAnnotations];
Call this method after adding your annotation like this
-(void)zoomToAnnotations{
MKMapRect zoomRect = MKMapRectNull;
for (_yourAnnotation in self.yourMapView.annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(_yourAnnotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
}else{
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
if (zoomRect.size.width == 0.10) /* for single annotation available in map */
{
zoomRect = MKMapRectMake(zoomRect.origin.x, zoomRect.origin.y, 100000, 100000);
}
[[self yourMapView] setVisibleMapRect:zoomRect edgePadding:UIEdgeInsetsMake(50, 50, 50, 50) animated:YES];
}

How to Set a Region for a MKMapView?

I am new in iOS apps development and I want to put a Map in My application with a pushpin on an exact location using its latitude and longitude. I add the Map but the problem is it always appear with its initial position an the push pin doesn't appear (It is on the map but you need to change the position to see it ) . Like this figures shows :
Initial Position
2.- After I moved the map's position
What I want is to show the position of the pushpin as a default location and with a zoom .
This is a sample of My Code :
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D myCoordinate = {20.5, -7.06}; //AS an Example
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate;
//Drop pin on map
[self.mapView addAnnotation:point];
//Region with Zoom
-(void)viewWillAppear:(BOOL)animated {
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 20.5;
zoomLocation.longitude= -7.6;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
[self.mapView setRegion:viewRegion animated:YES];
}
I used this code but still the same thing it always display the initial position (Figure 1)
Thank You .
(void)viewDidLoad
{
[super viewDidLoad];
MapAnnotation *ann = [[MapAnnotation alloc] init];
MKCoordinateRegion region;
region.center.latitude = 31.504679;
region.center.longitude = 74.247429;
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
[mapView setRegion:region animated:YES];
ann.title = #"Digital Net";
ann.subtitle = #"Office";
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
(MKAnnotationView *) mapView:(MKMapView *)mpView viewForAnnotation:(id)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation) {
static NSString *defaultID = #"myLocation";
pinView = (MKPinAnnotationView *)[mpView dequeueReusableAnnotationViewWithIdentifier:defaultID];
if(pinView == nil) {
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultID];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
}
return pinView;
}
I have done something like that and it was working fine

Draw Rectangle on the MapView

My application gets data in the following coverage area. I would like to use these boundary values to draw rectangle on the map. Do you have any idea? Thanks in advance
Left Upper Boundary: 30.439217,-95.899668;
Right Upper Boundary: 30.443953,-94.685679;
Left Buttom boundary: 28.930662,-95.908595;
Right Buttom Boundary: 28.930061,-94.690486;
if you wanna draw something basic with lines u can use this MKPolyline
Try the code below, Make sure your delegate is connected with your mapview object
- (void) drawRect
{
CLLocation *coordinates1 = [[CLLocation alloc] initWithLatitude:30.439217 longitude:-95.899668];
CLLocation *coordinates2 = [[CLLocation alloc] initWithLatitude:30.443953 longitude:-94.685679];
CLLocation *coordinates3 = [[CLLocation alloc] initWithLatitude:28.930061 longitude:-94.690486];
CLLocation *coordinates4 = [[CLLocation alloc] initWithLatitude:28.930662 longitude:-95.908595];
NSMutableArray *locationCoordinates = [[NSMutableArray alloc] initWithObjects:coordinates1,coordinates2,coordinates3,coordinates4,coordinates1, nil];
int numberOfCoordinates = [locationCoordinates count];
CLLocationCoordinate2D coordinates[numberOfCoordinates];
for (NSInteger i = 0; i < [locationCoordinates count]; i++) {
CLLocation *location = [locationCoordinates objectAtIndex:i];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[i] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
[self.myMapView addOverlay:polyLine];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 1.0;
return polylineView;
}
UPDATE: Here's the swift version of the drawRect function
func drawRect() {
let coordinates1 = CLLocation(latitude: 30.439217, longitude: -95.899668)
let coordinates2 = CLLocation(latitude: 30.443953, longitude: -94.685679)
let coordinates3 = CLLocation(latitude: 28.930061, longitude: -94.690486)
let coordinates4 = CLLocation(latitude: 28.930662, longitude: -95.908595)
let locationCoordinates = [coordinates1,coordinates2,coordinates3,coordinates4,coordinates1]
let coordinates = locationCoordinates.map { $0.coordinate }
let polyLine = MKPolyline(coordinates: coordinates, count: coordinates.count)
mapView.addOver(polyLine)
}
replace the
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay (deprecated)
with
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
Since you've asked a general question I can only give you a general hint at the answer.
MKMapView has a method convertCoordinate:toPointToView: which turns a coordinate to a point in the view. You can use this to draw on an overlay.
First:
#import <QuartzCore/QuartzCore.h>
Then do this where you want:
CLLocationCoordinate2D koor = CLLocationCoordinate2DMake(30.439217,-95.899668);
CLLocationCoordinate2D koor2 = CLLocationCoordinate2DMake(30.443953,-94.685679);
CLLocationCoordinate2D koor3 = CLLocationCoordinate2DMake(28.930662,-95.908595);
CGPoint point1 = [mapView convertCoordinate:koor toPointToView:mapView];
CGPoint point2 = [mapView convertCoordinate:koor2 toPointToView:mapView];
CGPoint point3 = [mapView convertCoordinate:koor3 toPointToView:mapView];
UIView *someView = [[UIView alloc]initWithFrame:CGRectMake(point1.x, point1.y, point2.x-point1.x, point3.y-point1.y)];
[self viewBicimle:someView];
[someView setBackgroundColor:[UIColor clearColor]];
[someView.layer setBorderColor:[[[UIColor blackColor] colorWithAlphaComponent:1.0] CGColor]];
[someView.layer setBorderWidth:1.0];
[mapView addSubview:someView];
[someView release];
But this will only work for an immovable some MapView. When you move the map it will slide...

Resources