1

I would like draw a filled polygon on iPhone with Google map (Version 1.1.1, the last one).

Anyone knows how to do like that on ios :

(My code on Android)

mMap.addPolygon(new PolygonOptions()
            .addAll(latLngList)
            .fillColor(Color.BLUE)
            .strokeColor(Color.RED)
            .strokeWidth(3));

Regards,

PS : If you have many solutions, keep in mind that I have many Polygon to draw.

Thomas Leduc
  • 1,092
  • 1
  • 20
  • 44

1 Answers1

1

The SDK currently doesn't support filled polygons, however there is a feature request to add them here:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=5070

In the meantime, one option could be to draw your polygons into an image, and then add them as a ground overlay. This would be very limiting, but might work as a temporary workaround.

Another option is to add another view over the top of the map view and draw the polygons into it, and then update them whenever the map view moves. It isn't possible to perfectly synchronize another view with the map view, so your polygons will lag behind a bit as you pan/zoom around, but this might also be okay for you as a temporary workaround.

UPDATE

These are just some random ideas to try for the ground overlay approach, I'm not sure if they would work, but they might get you started:

I would suggest converting the lat/lon corners of the rectangle into MKMapPoint (using MKMapPointForCoordinate). These are equivalent to Google's coordinate system at zoom level 20.

You can then use the aspect ratio of the width/height of the rectangle in MKMapPoint coordinates to determine the aspect ratio of your ground overlay UIImage. Once you have the aspect ratio, you'll just need to experiment with actual sizes (ie guess a width, calculate the height from the aspect ratio) to find one which looks okay. The bigger it is, the finer the detail of your rectangle will be, but the more memory it will use, and probably the slower the performance will be. Also you might hit a hard limit at some size - I'm guessing the UIImage gets converted by the Google Maps SDK into a texture, and textures have a max size of 2048x2048 on iPhone 3GS+.

Then, use something similar to How to setRegion with google maps sdk for iOS? to calculate a zoom level and centre lat/lon. Instead of the map view width/height you would use your UIImage width/height, and you'd use the bounds of your rectangle instead of the bounds of the desired view. You also wouldn't need to calculate the scale from both the width and height (as the scale should be the same) - so just use one of them. Instead of creating a camera with the zoom level and centre lat/lon, set them on the GMSGroundOverlayOptions. Also set the ground overlay's anchor to the centre of the image (ie 0.5, 0.5).

The above describes how to add one GroundOverlay per rectangle. If you have lots of overlapping or nearby rectangles you could probably combine them into a single UIImage, but that would be a bit more complicated.

Community
  • 1
  • 1
Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • Thanks Saxon, I have already seen you on Google Map Circle post. I have already take the solution of draw polygon in UIImage with CGContextFillRect and I'm testing the render of many groundedOverlay ... When you believe Google Maps for iOs team will make it possible ? – Thomas Leduc Mar 26 '13 at 08:17
  • And I get stuck dealing with this problem : If I want to draw a groundedOverlay with precise coordinate, how to know which size to give to the overlay image and which zoom to give to the option to match precisely my map ? – Thomas Leduc Mar 26 '13 at 09:24
  • Hi Thomas, what form are your polygons in? Are they a list of lat/lon, are they rectangles or just general irregular shapes? I have no idea if or when Google will release that feature request. They seem to be doing monthly releases (see https://developers.google.com/maps/documentation/ios/releases), so maybe another release will come out some time next month, but I don't know what they might choose to put in it. – Saxon Druce Mar 26 '13 at 14:48
  • Thanks for your answers. My polygons are simple rectangles, with background with alpha and border. I generate UIImage via CGContext but I don't understand how to give a right GCSize to my CGRect to match with the longitude/latitude on map. (Because I have to match with Android app via the server api info). – Thomas Leduc Mar 26 '13 at 22:34
  • Hi Thomas, are the rectangles defined as min/max lat/lon, or something else? – Saxon Druce Mar 27 '13 at 01:34
  • The rectangles are defined by : *-*-* ltp : CLLocationCoordinate2D -> the Left Top Point *-*-* width : _ltp.longitude_ - _rtp.longitude_ -> the difference of longitude *-*-* height : _ltp.latitude_ - _lbp.latitude_ -> the difference of latitude *-*-* So I can't find how to know _a_, _b_ in _CGSize(a, b)_ of UIImage form zoomLevel, width, height. Thanks to stay with me :p – Thomas Leduc Mar 27 '13 at 01:57
  • Hi Thomas, I've added an update with some ideas to try, good luck :) – Saxon Druce Mar 27 '13 at 02:29
  • Thnaks you very much for the MKMapPointForCoordinate trick, I gonna test it ! – Thomas Leduc Mar 27 '13 at 09:49
  • But @Saxon Druce, by chance, have you a blog with a sample code to do this ? Because, I'm late in my work and I'm sure, I'm not alone to get trouble with this. Anyway, you save my week, for the second time in the month ! – Thomas Leduc Mar 27 '13 at 10:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27000/discussion-between-thomas-leduc-and-saxon-druce) – Thomas Leduc Mar 27 '13 at 10:34
  • I have a CGSize but it is too large (2200 x 2200), if my UIImage is bigger than 900 x 900, the app crash. How can adapt zoom level for scaled UIImage ? – Thomas Leduc Mar 27 '13 at 17:44
  • Hi Thomas, sorry this isn't something I've tried to do myself, so I don't have any existing code on a blog or elsewhere. If you have something that's almost working, maybe post your code and I'll see if I can see anything that doesn't quite look right? To answer your last question, you could reduce the size of the UIImage to half, and then decrease the zoom level by 1. If the UIImage is still too large, halve the image / decrease the zoom level again, and repeat until you have a small enough UIImage size. – Saxon Druce Mar 28 '13 at 02:16
  • Thank you so much, I have took your other answer and I do this : - devide the square size by number - and had the log2(number) to the 20.0f of level zoom I don't know if I was lucky but, it seems ok. This post is **definitely answered** ! – Thomas Leduc Mar 28 '13 at 10:30