As you seen in the image, there are numbers of polygon on the top of the mapView. Each polygon overlays on the top of other polygon. This causes opacity problem and that misleads user to interpret colors by referring to colormap.
Before placing any polygons, first I want to remove/clear the new polygon area then add the polygon.
I hope my question clear! if not, please let me know. Appreciated in advance.
I have also add portion of my code below as a reference! Polygon data comes from server in JSON format and I get coordinates out of this data and add them as a polygon for each time stamp.
for(bb = 0; bb < [polygonArray count]; bb++){
coords = malloc(sizeof(CLLocationCoordinate2D) * [[polygonArray objectAtIndex:bb] count]);
for (int a = 0;a < [[polygonArray objectAtIndex:bb] count]; a++){
coords[a].latitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:0]doubleValue];
coords[a].longitude = [[[[polygonArray objectAtIndex:bb]objectAtIndex:a]objectAtIndex:1]doubleValue];
}
polygon = [[MKPolygon alloc]init];
polygon = [MKPolygon polygonWithCoordinates:coords count:[[polygonArray objectAtIndex:bb]count]];
[previousPolygons addObject:polygon];
[mapView addOverlay:polygon];
}
}
Hmm. I'm a little unclear on what you want to do. If you simply want to remove a polygon, you'd have to some how find the polygon you want remove and run
[mapView removeOverlay:polygon]
If you want to remove all polygons then you could run
[mapView removeOverlays:mapView.overlays]
Related
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.
I am using google map sdk for ios to provide directions between current user location and an end location. I have so far achieved to draw a GMSPolyline between the current user location and the end location using the code below and it's working great.
GMSPath *encodedPath = [GMSPath pathFromEncodedPath:encodedPathSting];
self.polyline = [GMSPolyline polylineWithPath:encodedPath];
self.polyline.strokeWidth = 4;
self.polyline.strokeColor = [UIColor colorWithRed:55.0/255.0 green:160.0/255.0 blue:250.0/255.0 alpha:1.0];;
self.polyline.map = self.mapView;
Is it possible to remove a part of the GMSPolyline that has been covered by the user through driving/walking? The GMSPolyline must gradually decrease in length as we trace the path.
One way to achieve this is by redrawing the path repeatedly but this is not or may not be efficient.
Thanks.
So get the latlng point of the polyline in an array as described here:
//route is the MKRoute in this example
//but the polyline can be any MKPolyline
NSUInteger pointCount = route.polyline.pointCount;
//allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D * routeCoordinates = malloc(pointCount * sizeof(CLLocationCoordinate2D));
//get the coordinates (all of them)...
[route.polyline getCoordinates: routeCoordinates
range: NSMakeRange(0, pointCount)
];
//this part just shows how to use the results...
NSLog(#"route pointCount = %d", pointCount);
for (int c = 0; c < pointCount; c++) {
NSLog(#"routeCoordinates[%d] = %f, %f",
c, routeCoordinates[c].latitude, routeCoordinates[c].longitude);
}
//free the memory used by the C array when done with it...
free(routeCoordinates);
Then, implement a while loop for the first point as you move along the path like this:
int c = 0;
while (pointCount.size() > 0)
{
pointCount.get(0).remove();
}
Note: I'm not that experienced with iOS and haven't tested this solution. Treat it as a suggestion rather than a fix. Thanks!
Hope it helps!
I need to send lat/lng coordinates to a 3rd party API and it doesn't look like it's possible to extract lat/lng coordinates from a GMSCircle, so
I was thinking about generating an array of CGPoints and using GMSProjection to extract the location coordinates, however the radius needs to be exactly 1.5 miles; so with regard to the zoom level would I need to resort to something like this: https://gis.stackexchange.com/questions/7430/what-ratio-scales-do-google-maps-zoom-levels-correspond-to to calculate it?
Is there a better way to go about this? I feel like this would be a pretty common use case
Figured it out, I ended up using the GMSGeometryUtils GMSGeometryOffset function to get the offset from my origin and make a circle.
NSMutableArray *path = [NSMutableArray array];
for (int i = 0; i < 60; i++) {
CLLocationCoordinate2D locationCoordinate = GMSGeometryOffset(location.coordinate, 2414.02, i*6);
[path addObject:[[CLLocation alloc] initWithLatitude:locationCoordinate.latitude longitude:locationCoordinate.longitude]];
}
I wants to draw polyline for walk like google map app in ios using google maps ios sdk. For more clear understanding i am uploading image that is from google map app(ios.)
Are you asking how to achieve the dotted polyline effect? If so, I don't believe that is supported in the SDK.
You can manually create a similar effect with GMSCircles.
for(int x = 0; x < [self.path count]; x++)
{
CLLocationCoordinate2D coord = [self.path coordinateAtIndex:x];
//draw circle coord
GMSCircle *circle = [GMSCircle circleWithPosition:coord radius:20];
circle.fillColor = [UIColor blueColor];
circle.strokeColor = [UIColor blackColor];
circle.strokeWidth = 2;
circle.map = mapView;
}
For this to really look like the original example you will probably need to add additional points onto the line in order for the circles to be evenly spaced out. For that you could do something like this.
for(all the points in the path)
{
if(the distance from pointA to pointB is > some distance)
{
centerPtr = center point of pointA and pointB
insert centerPt in path
}
}
You can turn this into a simple recursive function that should give you something similar to what you are looking for.
Is it possible to check if a part of GMSPath is in a visible region?
The path is made up of coordinates. And the mapview has a visible Region.
You can easily check if a coordinate is in a region eithout even going to pixelspace:
- (void)checkPath:(GMSPath*)path {
GMSVisibleRegion visibleRegion = _googleMap.projection.visibleRegion;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion];
for(int i = 0; i < path.count; i++) {
CLLocationCoordinate2D coordinate=[path coordinateAtIndex:i];
if([bounds containsCoordinate:coordinate]) {
NSLog("Visible");
}
}
}
It depends on how accurate you need it to be.
Daij-Djan's answer uses an axis-aligned bounding box of the visible region, which will be bigger than the actual region if the view is rotated / tilted.
Sunny Shah's answer will be more accurate as it will fit exactly to the visible region of the view. However it will probably be slower as it has to project each point into screen coordinates.
Both of these answers only check if a point on the path is within the visible region. If you have a line in the path which crosses the visible region but the two vertices are outside of the visible region, both of these answers will report the path as being invisible. To solve this you would need some kind of line-vs-box collision test.
Use the pointForCoordinate method of the mapview to see if a given point of the path would be on the screen
for (int i = 0; i < path.count; i++) {
CLLocationCoordinate2D coordinate=[path coordinateAtIndex:i];
CGPoint markerPoint = [mapView_.projection pointForCoordinate:coordinate];
if (markerPoint.x >= 0 && markerPoint.y >= 0 && markerPoint.x <= mapView_.frame.size.width && markerPoint.y <= mapView_.frame.size.height) {
NSLog(#"Visible");
}
}