To draw polygon on google map with MapKit framework - ios

I wanted to display Google map in a map view on which I want to draw a polygon/circle.
Any advice?

The way I'm reading your question is that you want to programmatically draw the polygon on the map. For this, consult the Apple docs on MapKit.
You don't need to add transparent views over the MapKit map (MKMapView). You create an overlay object, in this case an MKPolygon. (in the following example, the variable map will be the MKMapView instance owned by the view controller that you put this code in):
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);
MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
poly.title = #"Colorado";
[map addOverlay:poly];
Then, if you want to customize the look (colors, stroke, etc.) of the overlay, you implement the MKMapViewDelegate protocol in the view controller you have that owns the MKMapView object and provide an implementation of mapView:viewForOverlay:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView* aView = [[[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay] autorelease];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
Of course, always remember to actually assign the map instance's delegate to your view controller (MKMapViewDelegate), either in the interface builder, or in code (e.g. viewDidLoad).

I used ideas from this persons blog post to accomplish this. It basically involves adding a transparent view over the map. The map then allows you to convert locations to points on the view. Let me know if the site does not help you and I can try and dig up an example from my code.
http://spitzkoff.com/craig/?p=65

Related

MKMapView Draw connected lines between all points

I have a list of 50+ coordinates. What is the most efficient way to draw lines between all these coordinates (should create a "circular" path because they all have a display order) that is also easy to customize (line thickness, color, etc...)?
Thanks!
I am not sure I understand your question for certain. If you are looking for a list of points to display from end to end, then you will want to create a MKPolyline object from those points, making sure the points are added to the myPoints array in the order you want to connect them:
CLLocationCoordinate2D coordinates[[myPoints count]];
int i = 0;
for (Checkpoint *point in myPoints)
{
coordinates[i] = CLLocationCoordinate2DMake([point.lat floatValue] , [point.lon floatValue]);
i++;
}
self.polyline = [MKPolyline polylineWithCoordinates:coordinates count: [myPoints count]];
[mapView addOverlay:self.polyline];
Then make sure you are implementing the delegate method - mapView:rendererForOverlay:. Here's an example, but tailor it to your needs:
-(MKOverlayRenderer*)mapView:(MKMapView*)mapView rendererForOverlay:(id <MKOverlay>)overlay
{
MKPolylineRenderer* lineView = [[MKPolylineRenderer alloc] initWithPolyline:self.polyline];
lineView.strokeColor = [UIColor blueColor];
lineView.lineWidth = 7;
return lineView;
}
However, if you really want a closed loop (circular) object, then you will want to create a MKPolygon object instead. The process is quite similar; in that case replace the self.polyline initializer above with this code:
self.polygon = [MKPolygon polygonWithCoordinates:coordinates count: [myPoints count]];
[mapView addOverlay:self.polygon];
The - mapView:rendererForOverlay: code should remain the same I think. I haven't tested this code, but hopefully it gets you moving in the right direction.

How to draw path with MapKit in real time (iOS7+)

I'm trying to figure out how to draw a path with MapKit given an array of location (lat/long) points. I think I need to use MKPolyline and MKOverlayRenderer. I can only seem to find information on MKRoute and MKDirections, but this is not what I need.
To start with I will have one point. Every 10 seconds or so another point will be added. How can I draw a line on a map in real time given an array of points, where the array grows over time?
This is all I have so far:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolyline *route = overlay;
MKPolylineRenderer *routeRenderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
routeRenderer.strokeColor = [UIColor blueColor];
return routeRenderer;
}
return nil;
}
You should be able to do this with MKPolyline and MKPolylineRenderer as you have, but also look into -[MKOverlayRenderer setNeedsDisplayInMapRect:] and the associated conversion routines to translate MKMapRect to CGRect (if necessary) and you should be able to force a screen refresh upon receipt of new data.

How to add circular region onto MKMapView

I am trying to add a circular region on my MKMapView like the picture below for my current location. I can pin the map with annotations but don't know how to get it to show a circular region like this with a radius and have it shaded.
So I figured it out...
Below is how I did it along with code.
Step 1: - Create MKCircle
MKCircle *circleOverlay = [MKCircle circleWithCenterCoordinate:zoomLocation radius:300];//radius in meters
Step 2: - Set title and add to Map Overlays
[circleOverlay setTitle:#"Circle1"];
[_mapView addOverlay:circleOverlay];
Step 3: - Implement the mapView:rendererForOverlay: method in my MapViewDelegate
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
if ([overlay isKindOfClass:[MKCircle class]])
{
MKCircleRenderer* aRenderer = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
aRenderer.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aRenderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aRenderer.lineWidth = 3;
return aRenderer;
}else{
return nil;
}
}
That was it! Boom! Hope it helps someone in the future! Not sure if this is the best way but it achieve my goal!
Edit: make sure to set your mapView's delegate to self or the required delegate method will not be called.

MKMapView overlay Polyline is drawn on top of road label

I am making the navigation application by drawing a direction between 2 points. I successfully archive the functionality.
But the direction line is drawn on top of the road label and that label cannot be read as show in the picture below.
this is my code to draw overlay
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithPolyline:overlay];
overlayView.lineWidth = 10.0f;
//overlayView.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.5f];
overlayView.strokeColor = [UIColor redColor];
return overlayView;
}
I can overcome this with a transparent line but it is not the efficient way.
The best way is to draw the line between the map layer and label layer of MKMapView but i don't know how can i archive that.
So any help please.
Thanks.
Assuming you are writing this for iOS maps (not google maps as you tagged the question) and using iOS 7 then when you add the overlay to the map view you have the option of defining which level it is on addOverlay:level:. The levels are defined in the MKMapView class reference
[theMapView addOverlay:theOverlay level:MKOverlayLevelAboveRoads];

How to draw a path line on a map?

I am building an iOS app using Rubymotion.
I am trying to draw a line (path) on a map using coordinates.
I can run it in my app but I see no lines on the map (and no errors either).
#mapview = MKMapView.alloc.initWithFrame(view.bounds)
#mapview.mapType = MKMapTypeStandard
#mapview.delegate = self
#mapview.showsUserLocation = true
#mapview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
view.addSubview(#mapview)
Then I try to draw the line
path = [CLLocationCoordinate2D.new(41.878114,-87.629798), CLLocationCoordinate2D.new(41.865947,-87.622576)]
pointers = Pointer.new(CLLocationCoordinate2D.type, path.length)
pointers[0] = path[0]
pointers[1] = path[1]
polyLine = MKPolyline.polylineWithCoordinates(pointers, count:2)
#mapview.addOverlay(polyLine)
I'm no expert of ruby, but in order to actually see an overlay view on a map you need to set your class as the map's delegate and implement the – mapView:viewForOverlay: protocol method, where you need to return the actual MKPolyLineView object for the map to show!
In Objective C I would simply implement this method as:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay
{
if ( [overlay isKindOfClass:[MKPolyLine class]] {
MKPolyLineView *polyView = [[MKPolyLineView alloc] initWithPolyline:overlay];
return polyView;
}
}
Hope this helps!

Resources