Draw Rectangle on the MapView - ios

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...

Related

Why This Code Does Not Draw Polyline on MKMapView

I have the following code, with which i am trying to draw a polyline between a set of coordinates (which are correct as I also use them to add pins to the map, and those work fine).
I call a drawing method to initiate the drawing like so (the array in the method call contains the necessary coordinates):
[self drawRoute:[[transportData objectForKey:#"19"] objectForKey:#"stops"]];
This is the actual method that is supposed to draw the line on the map (selectedRoute is an MKPolyline object):
- (void)drawRoute:(NSArray *)routePointsArray {
if (selectedRoute) {
[mapView removeOverlay:selectedRoute];
selectedRoute = nil;
}
CLLocationCoordinate2D routeCoordinates[routePointsArray.count];
for (int i = 0; i < routePointsArray.count; i++) {
float latitude = [[[routePointsArray objectAtIndex:i] objectForKey:#"lat"] floatValue];
float longitude = [[[routePointsArray objectAtIndex:i] objectForKey:#"lon"] floatValue];
CLLocationCoordinate2D routePoint = CLLocationCoordinate2DMake(latitude, longitude);
routeCoordinates[i] = routePoint;
}
selectedRoute = [MKPolyline polylineWithCoordinates:routeCoordinates count:routePointsArray.count];
[mapView addOverlay:selectedRoute];
[mapView setVisibleMapRect:[selectedRoute boundingMapRect]];
}
And this is my delegate:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
if(overlay == selectedRoute)
{
if(nil == routeLineView)
{
routeLineView = [[MKPolylineRenderer alloc] initWithPolyline:selectedRoute];
routeLineView.fillColor = [UIColor redColor];
routeLineView.strokeColor = [UIColor redColor];
routeLineView.lineWidth = 5;
}
return routeLineView;
}
return nil;
}
I kind of narrowed it down to the routeCoordinates array not getting filled up with coordinates, but I do not understand why.
Also, if you spot any mistakes in the code I would really appreciate if you could point those out to me (possibly with a solution) as I am just learning this part of iOS and can use any help I can get.
You have an error in your rendererForOverlay method.
The first thing it does is assign an instance of MKPolylineRenderer to routeLineView, but later you only actually add the overlay if routeLineView is nil, which it won't be.
Remove the line that assigns the initial value to routeLineView.

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?

not able to add annotation to mapView iOS from a different method

I'm trying to add a annotation to the map from a different button method, however it doesn't show up. It doesn't give an error either.
Where as all the annotations which I have mentioned in the mapView method, they are being displayed.
-(void) testButton {
/// custom location coordinate
CLLocationCoordinate2D userLocationNew2 = CLLocationCoordinate2DMake(-80.050003, -13.416667);
// use that cusom coordinate
MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(userLocationNew2, 20000, 20000);
[mapView setRegion:[self.mapView regionThatFits:region2] animated:YES];
// Add an annotation
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate = userLocationNew2;
point2.title = #"Check";
// point2.subtitle = #"Right here oooooo!!!";
[mapView addAnnotation:point2];
}
The other method
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
/// custom location coordinate
CLLocationCoordinate2D userLocationNew2 = CLLocationCoordinate2DMake(-73.050003, -13.416667);
// use that cusom coordinate
MKCoordinateRegion region2 = MKCoordinateRegionMakeWithDistance(userLocationNew2, 20000, 20000);
[self.mapView setRegion:[self.mapView regionThatFits:region2] animated:YES];
// Add an annotation
MKPointAnnotation *point2 = [[MKPointAnnotation alloc] init];
point2.coordinate = userLocationNew2;
point2.title = #"Basic";
}

Plot pin does not point exact place on MKMapView

- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.myMapView addGestureRecognizer:tapRecognizer];
}
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.myMapView];
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
point1.coordinate = tapPoint;
[self.myMapView addAnnotation:point1];
}
I used above code to make pin point on MKMapView when i choose a place in MKMapView it does not point where i exactly touches.It goes little far where i touches.What is wrong with my code?.any help will be appreciated.thanks in advance.
Try changing the passed-in object to the locationInView: method to self.view:
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.view];
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
point1.coordinate = tapPoint;
[self.myMapView addAnnotation:point1];
}
I know the other answer is working for you, but I just wanted to show the proper way to use convertPoint:toCoordinateFromView:. Instead of passing it the container view, it should be passed the map view that the point is in:
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point
toCoordinateFromView:self.myMapView];
That saves swapping between views. Here's the full method:
-(void)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.myMapView];
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.myMapView];
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
point1.coordinate = tapPoint;
[self.myMapView addAnnotation:point1];
}

Circle Overlay is not showing on map

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.

Resources