To draw polygon on google map with MapKit framework

廉价感情. 提交于 2019-12-19 03:45:12

问题


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

Any advice?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/1334133/to-draw-polygon-on-google-map-with-mapkit-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!