Inverted Polygon in GMSMapView

孤街醉人 提交于 2021-02-07 12:19:32

问题


I have to use Google Map for my iPhone project and I'am using GMSPolygon to draw a polygon but How can I fill everywhere on my map except the interior of the polygon. Like the image below.

Thank You.

sample


回答1:


I have played with your question. Main idea is to fill all Earth with polygon and then make holes for your particular area. Here is example code, but problem is that I can`t create polygon to cover whole Earth. First approach is to create 4 polygons for each quarter.

PGeoCountry is a struct with desired areas in boundariesPaths:[GMSPath]

func locateCountry(country: PGeoCountry) {
    // 1. Create one quarter earth filling polygon
    let fillingPath = GMSMutablePath()
    fillingPath.addLatitude(90.0, longitude: -90.0)
    fillingPath.addLatitude(90.0, longitude: 90.0)
    fillingPath.addLatitude(0, longitude: 90.0)
    fillingPath.addLatitude(0, longitude: -90.0)

    let fillingPolygon = GMSPolygon(path:fillingPath)
    let fillColor = UIColor(red: 0.6, green: 0.6, blue: 0.6, alpha: 0.6)
    fillingPolygon.fillColor = fillColor
    fillingPolygon.map = self.mapView

    // 2. Add prepared array of GMSPath
    fillingPolygon.holes = country.boundariesPaths

    // 3. Add lines for boundaries
    for path in country.boundariesPaths {
        let line = GMSPolyline(path: path)
        line.map = self.mapView
        line.strokeColor = UIColor.blackColor()
    }

}

Here is the result:




回答2:


-(void) drawsamplePolygon
      {
  GMSPolygon *polygon = [[GMSPolygon alloc] init];
        polygon.path = [self pathOfNewYorkState];
        polygon.title = @"New York";
        polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.2f];
        polygon.strokeColor = [UIColor blackColor];
        polygon.strokeWidth = 2;
        polygon.tappable = YES;
        polygon.map = gMapView;

    }
    - (GMSPath *)pathOfNewYorkState {
        GMSMutablePath *path = [GMSMutablePath path];
        [path addLatitude:42.5142 longitude:-79.7624];
        [path addLatitude:42.7783 longitude:-79.0672];
        [path addLatitude:42.8508 longitude:-78.9313];
        [path addLatitude:42.9061 longitude:-78.9024];
        [path addLatitude:42.9554 longitude:-78.9313];
        [path addLatitude:42.9584 longitude:-78.9656];
        [path addLatitude:42.9886 longitude:-79.0219];
        [path addLatitude:43.0568 longitude:-79.0027];
        [path addLatitude:43.0769 longitude:-79.0727];
        [path addLatitude:43.1220 longitude:-79.0713];
        [path addLatitude:43.1441 longitude:-79.0302];
        [path addLatitude:43.1801 longitude:-79.0576];
        [path addLatitude:43.2482 longitude:-79.0604];
        [path addLatitude:43.2812 longitude:-79.0837];
        [path addLatitude:43.4509 longitude:-79.2004];
        [path addLatitude:43.6311 longitude:-78.6909];
        [path addLatitude:43.6321 longitude:-76.7958];
        [path addLatitude:43.9987 longitude:-76.4978];
        [path addLatitude:44.0965 longitude:-76.4388];
        [path addLatitude:44.1349 longitude:-76.3536];
        [path addLatitude:44.1989 longitude:-76.3124];
        [path addLatitude:44.2049 longitude:-76.2437];
        [path addLatitude:44.2413 longitude:-76.1655];
        [path addLatitude:44.2973 longitude:-76.1353];
        [path addLatitude:44.3327 longitude:-76.0474];
        [path addLatitude:44.3553 longitude:-75.9856];
        [path addLatitude:44.3749 longitude:-75.9196];
        [path addLatitude:44.3994 longitude:-75.8730];
        [path addLatitude:44.4308 longitude:-75.8221];
        [path addLatitude:44.4740 longitude:-75.8098];
        [path addLatitude:44.5425 longitude:-75.7288];
        [path addLatitude:44.6647 longitude:-75.5585];
        [path addLatitude:44.7672 longitude:-75.4088];
        [path addLatitude:44.8101 longitude:-75.3442];
        [path addLatitude:44.8383 longitude:-75.3058];
        [path addLatitude:44.8676 longitude:-75.2399];
        [path addLatitude:44.9211 longitude:-75.1204];
        [path addLatitude:44.9609 longitude:-74.9995];
        [path addLatitude:44.9803 longitude:-74.9899];
        [path addLatitude:44.9852 longitude:-74.9103];
        [path addLatitude:45.0017 longitude:-74.8856];
        [path addLatitude:45.0153 longitude:-74.8306];
        [path addLatitude:45.0046 longitude:-74.7633];
        [path addLatitude:45.0027 longitude:-74.7070];
        [path addLatitude:45.0007 longitude:-74.5642];
        [path addLatitude:44.9920 longitude:-74.1467];
        [path addLatitude:45.0037 longitude:-73.7306];
        [path addLatitude:45.0085 longitude:-73.4203];
        [path addLatitude:45.0109 longitude:-73.3430];
        [path addLatitude:44.9874 longitude:-73.3547];
        [path addLatitude:44.9648 longitude:-73.3379];
        [path addLatitude:44.9160 longitude:-73.3396];
        [path addLatitude:44.8354 longitude:-73.3739];
        [path addLatitude:44.8013 longitude:-73.3324];
        [path addLatitude:44.7419 longitude:-73.3667];
        [path addLatitude:44.6139 longitude:-73.3873];
        [path addLatitude:44.5787 longitude:-73.3736];
        [path addLatitude:44.4916 longitude:-73.3049];
        [path addLatitude:44.4289 longitude:-73.2953];
        [path addLatitude:44.3513 longitude:-73.3365];
        [path addLatitude:44.2757 longitude:-73.3118];
        [path addLatitude:44.1980 longitude:-73.3818];
        [path addLatitude:44.1142 longitude:-73.4079];
        [path addLatitude:44.0511 longitude:-73.4367];
        [path addLatitude:44.0165 longitude:-73.4065];
        [path addLatitude:43.9375 longitude:-73.4079];
        [path addLatitude:43.8771 longitude:-73.3749];
        [path addLatitude:43.8167 longitude:-73.3914];
        [path addLatitude:43.7790 longitude:-73.3557];
        [path addLatitude:43.6460 longitude:-73.4244];
        [path addLatitude:43.5893 longitude:-73.4340];
        [path addLatitude:43.5655 longitude:-73.3969];
        [path addLatitude:43.6112 longitude:-73.3818];
        [path addLatitude:43.6271 longitude:-73.3049];
        [path addLatitude:43.5764 longitude:-73.3063];
        [path addLatitude:43.5675 longitude:-73.2582];
        [path addLatitude:43.5227 longitude:-73.2445];
        [path addLatitude:43.2582 longitude:-73.2582];
        [path addLatitude:42.9715 longitude:-73.2733];
        [path addLatitude:42.8004 longitude:-73.2898];
        [path addLatitude:42.7460 longitude:-73.2664];
        [path addLatitude:42.4630 longitude:-73.3708];
        [path addLatitude:42.0840 longitude:-73.5095];
        [path addLatitude:42.0218 longitude:-73.4903];
        [path addLatitude:41.8808 longitude:-73.4999];
        [path addLatitude:41.2953 longitude:-73.5535];
        [path addLatitude:41.2128 longitude:-73.4834];
        [path addLatitude:41.1011 longitude:-73.7275];
        [path addLatitude:41.0237 longitude:-73.6644];
        [path addLatitude:40.9851 longitude:-73.6578];
        [path addLatitude:40.9509 longitude:-73.6132];
        [path addLatitude:41.1869 longitude:-72.4823];
        [path addLatitude:41.2551 longitude:-72.0950];
        [path addLatitude:41.3005 longitude:-71.9714];
        [path addLatitude:41.3108 longitude:-71.9193];
        [path addLatitude:41.1838 longitude:-71.7915];
        [path addLatitude:41.1249 longitude:-71.7929];
        [path addLatitude:41.0462 longitude:-71.7517];
        [path addLatitude:40.6306 longitude:-72.9465];
        [path addLatitude:40.5368 longitude:-73.4628];
        [path addLatitude:40.4887 longitude:-73.8885];
        [path addLatitude:40.5232 longitude:-73.9490];
        [path addLatitude:40.4772 longitude:-74.2271];
        [path addLatitude:40.4861 longitude:-74.2532];
        [path addLatitude:40.6468 longitude:-74.1866];
        [path addLatitude:40.6556 longitude:-74.0547];
        [path addLatitude:40.7618 longitude:-74.0156];
        [path addLatitude:40.8699 longitude:-73.9421];
        [path addLatitude:40.9980 longitude:-73.8934];
        [path addLatitude:41.0343 longitude:-73.9854];
        [path addLatitude:41.3268 longitude:-74.6274];
        [path addLatitude:41.3583 longitude:-74.7084];
        [path addLatitude:41.3811 longitude:-74.7101];
        [path addLatitude:41.4386 longitude:-74.8265];
        [path addLatitude:41.5075 longitude:-74.9913];
        [path addLatitude:41.6000 longitude:-75.0668];
        [path addLatitude:41.6719 longitude:-75.0366];
        [path addLatitude:41.7672 longitude:-75.0545];
        [path addLatitude:41.8808 longitude:-75.1945];
        [path addLatitude:42.0013 longitude:-75.3552];
        [path addLatitude:42.0003 longitude:-75.4266];
        [path addLatitude:42.0013 longitude:-77.0306];
        [path addLatitude:41.9993 longitude:-79.7250];
        [path addLatitude:42.0003 longitude:-79.7621];
        [path addLatitude:42.1827 longitude:-79.7621];
        [path addLatitude:42.5146 longitude:-79.7621];
        return path;
    }



回答3:


You need to set default polygon to World map. and set hole property of that polygon which area you want to deselect. This will work.



来源:https://stackoverflow.com/questions/20772500/inverted-polygon-in-gmsmapview

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