Remove Multiple Polygon On google Map Objective C - ios

I am developing Iphone app and using Google map.Draw Multiple Polygon on google Map. but than deleting the polygon on map that deleting the last polygon not delete all draw polygon on google map.please help thanks in advance.
code..
// Draw Polygon
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (buttonClicked == true) {
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
_latLongDict = #{#"lat":#(coordinate.latitude), #"long":#(coordinate.longitude)};
[_clickCoordinate addObject:_latLongDict];
NSLog(#"%#",_clickCoordinate);
_path = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (NSDictionary *dic in _clickCoordinate) {
event.latitude = [[dic valueForKey:#"lat"] floatValue];
event.longitude = [[dic valueForKey:#"long"] floatValue];
[_path addCoordinate:event];
}
_polygun = [GMSPolygon polygonWithPath:_path];
_polygun.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
_polygun.strokeColor = [UIColor blackColor];
_polygun.strokeWidth = 2;
_polygun.map = mapView;
}
// Delete Polygon
- (IBAction)cancelButton:(id)sender {
for (int i = 0; i < _path.count; i++) {
_polygun.map = nil;
[_clickCoordinate removeAllObjects];
[_path removeAllCoordinates];
NSLog(#"%#",_clickCoordinate);
}
}

Related

How to insert array values to draw polygon based on long/lat values - Google Maps iOS?

I am trying the GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect]; method to draw a polygon of the tapped coordinates by the user. Here's how I store the clicked coordinates:
// array made of clicked coordinates
NSMutableArray *latitudeTappedCoordinates = [NSMutableArray array];
NSMutableArray *longitudeTappedCoordinates = [NSMutableArray array];
NSUInteger numberOfLongitudeCoordinates = [longitudeTappedCoordinates count];
NSUInteger numberOfLatitudeCoordinates = [latitudeTappedCoordinates count];
for (int i = 2; i < numberOfLatitudeCoordinates; i++) {
[latitudeTappedCoordinates addObject:[NSNumber numberWithInt:coordinate.latitude]];
}
for (int i = 2; i < numberOfLongitudeCoordinates; i++) {
[longitudeTappedCoordinates addObject:[NSNumber numberWithInt:coordinate.longitude]];
}
After this, I have the following:
// polygon
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
As you can see, the line
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
only takes in a single attribute. I want it to take in all the values in the arrays init above, so it can draw the polygon. How can I do that?
First store float values not int values in array.
then
Add in interface
NSMutableArray *latitudeTappedCoordinates;
NSMutableArray *longitudeTappedCoordinates;
You can do like this :
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
CLLocationCoordinate2D event;
for (int i = 0; i <= [longitudeTappedCoordinates count]-1; i++) {
event.latitude = [[latitudeTappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[longitudeTappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;

Marker does not add when it was dragged - Google Maps iOS app

I am developing an iOS Google Map. I have the following working except that when the user drags a marker, the marker doesn't get drawn there once released. Where have I made a mistake?
- (void)clearAllOverlay
{
[_mapView clear];
[self drawPolygon];
[self addMarker];
}
- (void)addMarker
{
CLLocationCoordinate2D position;
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = position;
marker.map = _mapView;
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
[marker setDraggable: YES];
}
}
//runs everytime user taps on marker
- (bool)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *) marker
{
[latitudeTappedCoordinates removeObject:[NSNumber numberWithFloat:marker.position.latitude]];
[longitudeTappedCoordinates removeObject:[NSNumber numberWithFloat:marker.position.longitude]];
[self clearAllOverlay];
return 0;
}
//called while marker is dragged
- (void)mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *) marker
{
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.longitude]];
[self clearAllOverlay];
}
//runs everytime user taps on any coordinate, except marker
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
// store tapped coordinates in multi-dimensional array
NSArray *array = #[[NSNumber numberWithFloat:coordinate.latitude], [NSNumber numberWithFloat:coordinate.longitude]];
[tappedCoordinates addObject:array];
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
[self clearAllOverlay];
}
Try this :
- (void) mapView:(GMSMapView *) mapViewdidEndDraggingMarker:(GMSMarker *) marker {
// store tapped coordinates in multi-dimensional array
NSArray *array = #[[NSNumber numberWithFloat:marker.position.latitude], [NSNumber numberWithFloat:marker.position.longitude]];
[tappedCoordinates addObject:array];
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.longitude]];
[self clearAllOverlay];
}

how to draw an arrow between two points on the map? MapKit

How to draw an arrow between two points on the map?
I try calc latitude and longitude, but something goes wrong.
Best regards, Max
This is my code and result picture
int currpoints1 = 2;
NSLog(#"!!!!!!!!%d ",numPoints);
while(currpoints1 < numPoints)
{
TrackPoint* current = nil;
CLLocationCoordinate2D* coordsArrrow = malloc((3) * sizeof(CLLocationCoordinate2D));
for (int i =currpoints1, j=0; i < numPoints; i=i+1, j++)
{
current = [cashpoints objectAtIndex:i];
coordsArrrow[j] = current.coordinate;
if (i % 2 !=0) {
int Gug = 30;
int ug;
float bx, by, ex, ey;
bx = coordsArrrow[0].latitude;by = coordsArrrow[0].longitude;
ex = coordsArrrow[1].latitude;ey = coordsArrrow[1].longitude;
float Lstr = sqrt((ex-ey)*(ex-ey)+(bx-by)*(bx-by));
ug = [self RetGradW:(abs(coordsArrrow[1].latitude-coordsArrrow[0].latitude)) height:abs(coordsArrrow[1].longitude-coordsArrrow[0].longitude)];
ug = ug - Gug;
coordsArrrow[0].latitude = ex;
coordsArrrow[0].longitude = ey;
coordsArrrow[1].latitude = ex+Lstr*cos(ug*M_PI/180);
coordsArrrow[1].longitude = ey+Lstr*sin(ug*M_PI/180);
ug=ug+2*Gug;
coordsArrrow[2].latitude = ex+Lstr*cos(ug*M_PI/180);
coordsArrrow[2].longitude = ey+Lstr*sin(ug*M_PI/180);
MKPolyline *points = [MKPolyline polylineWithCoordinates:coordsArrrow count:3];
points.subtitle = #"arrow";
[map addOverlay:points];
break;
}
}
free(coordsArrrow);
currpoints1 = currpoints1 +14;
}
Oh guys. I change other way. Make annotation and rotate with direction between two point.
metods :
calc direction:
int currpoints1 = 2;
NSLog(#"!!!!!!!!%d ",numPoints);
while(currpoints1 < numPoints)
{
TrackPoint* current = nil;
CLLocationCoordinate2D* coordsArrrow = malloc((2) * sizeof(CLLocationCoordinate2D));
for (int i =currpoints1, j=0; i < numPoints; i=i+1, j++)
{
current = [cashpoints objectAtIndex:i];
coordsArrrow[j] = current.coordinate;
if (i % 2 !=0) {
DirectAnnotation *placemark=[[DirectAnnotation alloc] initWithCoordinate: coordsArrrow[0]];
CLLocationCoordinate2D coord1 = coordsArrrow[0];
CLLocationCoordinate2D coord2 = coordsArrrow[1];
CLLocationDegrees deltaLong = coord2.longitude - coord1.longitude;
CLLocationDegrees yComponent = sin(deltaLong) * cos(coord2.latitude);
CLLocationDegrees xComponent = (cos(coord1.latitude) * sin(coord2.latitude)) - (sin(coord1.latitude) * cos(coord2.latitude) * cos(deltaLong));
CLLocationDegrees radians = atan2(yComponent, xComponent);
CLLocationDegrees degrees = radiansToDegrees(radians) + 360;
self.dir = fmod(degrees, 360);
NSLog(#"%f,%f %f,%f",coordsArrrow[0].latitude,coordsArrrow[0].longitude,coordsArrrow[1].latitude,coordsArrrow[1].longitude);
[self.map addAnnotation:placemark];
break;
}
}
free(coordsArrrow);
currpoints1 = currpoints1 +14;
}
and
annotation:
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation{
if ([annotation isKindOfClass:[DirectAnnotation class]]) {
arrow=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"parkingloc"];
arrow.image = [UIImage imageNamed:#"userLocationCompass.png"];
CGAffineTransform transform = CGAffineTransformMakeRotation(degreesToRadians(dir));
arrow.transform = transform;
return arrow;
}else {
return nil;
}}
But i have some problem
before zoom:
after zoom :
direction is confused.
Your arrows change the angle because you don't reuse them in viewForAnnotation correctly.
Just make some unique id for each arrow like this in viewForAnnotation:
routeAnnotationView = (MKAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:[NSString stringWithFormat:#"%f%f",annotation.coordinate.latitude, annotation.coordinate.longitude]];
if (!routeAnnotationView)
{
routeAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[NSString stringWithFormat:#"%f%f",annotation.coordinate.latitude, annotation.coordinate.longitude]];
UIImage *image = [self rotateImage:[UIImage imageNamed:#"arrpix11.png"] onDegrees:RADIANS_TO_DEGREES(curAngle)+90];
routeAnnotationView.image = image;
}
else
{
routeAnnotationView.annotation = annotation;
}
routeAnnotationView.canShowCallout = NO;
Use this for draw path between two location in Map.
http://code.google.com/p/ashiphone/downloads/detail?name=MapWithRoutes.zip&can=2&q=

unable to draw mkpolyline

I have used MKMapView to show map and current user location which is working correctly.
Now I want to draw a polyline as user moves but it is not working I tried follwing code:
for(int i = 0; i < [longarray count]; i++)
{
NSNumber *latt=[latarray objectAtIndex:i];
NSNumber *lonn=[longarray objectAtIndex:i];
sklat =[[NSString stringWithFormat:#"%#",latt]doubleValue];
sklongi =[[NSString stringWithFormat:#"%#",lonn]doubleValue];
CLLocationCoordinate2D coordinate1 = CLLocationCoordinate2DMake(sklat,sklongi);
// break the string down even further to latitude and longitude fields.
MKMapPoint point = MKMapPointForCoordinate(coordinate1);
// if it is the first point, just use them, since we have nothing to compare to yet.
pointsArray[i] = point;
}
self.routeLine = [MKPolyline polylineWithPoints:pointsArray count:[latarray count]];
free(pointsArray);
[mapview addOverlay:self.routeLine];
then i usedd overlay function as
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.routeLine)
{
routeLineView = [[MKPolylineView alloc] initWithPolyline:self.routeLine] ;
routeLineView.fillColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.strokeColor = [UIColor colorWithRed:0.000 green:5.100 blue:0.100 alpha:1];
routeLineView.lineWidth = 4;
overlayView = routeLineView;
}
return overlayView;
}

Grouped/Ungroup MKAnnotation depending on the zoom level (And keep it fast)

I have a mapview that has a lot of annotations, most of them are very close to each other. What I want to do is similar to the Photo app on iOS where annotations are grouped when they are too close to each other and whenever you zoom out, they un grouped if they are too far apart.
I have seen this question already but the answer given isn't really what I was looking for.
I'm looking for either a library or a algorithm that I could implement myself.
One of Apple's WWDC 2011 session videos shows how to do exactly this. Go to https://developer.apple.com/videos/wwdc/2011/ (must be registered developer) and scroll to the video titled "Visualizing Information Geographically with MapKit". The basic idea is to use an offscreen map view to hold all of your annotations, and copy them to the onscreen map view as needed, making sure that you're not trying to show too many at once. It even does the nifty animation with the annotations as you zoom.
Have a look here in order to see the full answer. It contains both implementations for MapKit and Google Maps. The code is inspired by WWDC 2011 video and works very well in my app.
I post the code for MapKit here anyway but there are a few useful remarks in my other answer.
- (void)didZoom:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[self updateVisibleAnnotations];
}
}
- (void)updateVisibleAnnotations {
static float marginFactor = 2.0f;
static float bucketSize = 50.0f;
MKMapRect visibleMapRect = [self.mapView visibleMapRect];
MKMapRect adjustedVisibleMapRect = MKMapRectInset(visibleMapRect, -marginFactor * visibleMapRect.size.width, -marginFactor * visibleMapRect.size.height);
CLLocationCoordinate2D leftCoordinate = [self.mapView convertPoint:CGPointZero toCoordinateFromView:self.view];
CLLocationCoordinate2D rightCoordinate = [self.mapView convertPoint:CGPointMake(bucketSize, 0) toCoordinateFromView:self.view];
double gridSize = MKMapPointForCoordinate(rightCoordinate).x - MKMapPointForCoordinate(leftCoordinate).x;
MKMapRect gridMapRect = MKMapRectMake(0, 0, gridSize, gridSize);
double startX = floor(MKMapRectGetMinX(adjustedVisibleMapRect) / gridSize) * gridSize;
double startY = floor(MKMapRectGetMinY(adjustedVisibleMapRect) / gridSize) * gridSize;
double endX = floor(MKMapRectGetMaxX(adjustedVisibleMapRect) / gridSize) * gridSize;
double endY = floor(MKMapRectGetMaxY(adjustedVisibleMapRect) / gridSize) * gridSize;
gridMapRect.origin.y = startY;
while(MKMapRectGetMinY(gridMapRect) <= endY) {
gridMapRect.origin.x = startX;
while (MKMapRectGetMinX(gridMapRect) <= endX) {
NSSet *allAnnotationsInBucket = [self.allAnnotationMapView annotationsInMapRect:gridMapRect];
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
NSMutableSet *filteredAnnotationsInBucket = [[allAnnotationsInBucket objectsPassingTest:^BOOL(id obj, BOOL *stop) {
BOOL isPointMapItem = [obj isKindOfClass:[PointMapItem class]];
BOOL shouldBeMerged = NO;
if (isPointMapItem) {
PointMapItem *pointItem = (PointMapItem *)obj;
shouldBeMerged = pointItem.shouldBeMerged;
}
return shouldBeMerged;
}] mutableCopy];
NSSet *notMergedAnnotationsInBucket = [allAnnotationsInBucket objectsPassingTest:^BOOL(id obj, BOOL *stop) {
BOOL isPointMapItem = [obj isKindOfClass:[PointMapItem class]];
BOOL shouldBeMerged = NO;
if (isPointMapItem) {
PointMapItem *pointItem = (PointMapItem *)obj;
shouldBeMerged = pointItem.shouldBeMerged;
}
return isPointMapItem && !shouldBeMerged;
}];
for (PointMapItem *item in notMergedAnnotationsInBucket) {
[self.mapView addAnnotation:item];
}
if(filteredAnnotationsInBucket.count > 0) {
PointMapItem *annotationForGrid = (PointMapItem *)[self annotationInGrid:gridMapRect usingAnnotations:filteredAnnotationsInBucket];
[filteredAnnotationsInBucket removeObject:annotationForGrid];
annotationForGrid.containedAnnotations = [filteredAnnotationsInBucket allObjects];
[self.mapView addAnnotation:annotationForGrid];
//force reload of the image because it's not done if annotationForGrid is already present in the bucket!!
MKAnnotationView* annotationView = [self.mapView viewForAnnotation:annotationForGrid];
NSString *imageName = [AnnotationsViewUtils imageNameForItem:annotationForGrid selected:NO];
UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 2, 8, 8)];
[countLabel setFont:[UIFont fontWithName:POINT_FONT_NAME size:10]];
[countLabel setTextColor:[UIColor whiteColor]];
[annotationView addSubview:countLabel];
imageName = [AnnotationsViewUtils imageNameForItem:annotationForGrid selected:NO];
annotationView.image = [UIImage imageNamed:imageName];
if (filteredAnnotationsInBucket.count > 0){
[self.mapView deselectAnnotation:annotationForGrid animated:NO];
}
for (PointMapItem *annotation in filteredAnnotationsInBucket) {
[self.mapView deselectAnnotation:annotation animated:NO];
annotation.clusterAnnotation = annotationForGrid;
annotation.containedAnnotations = nil;
if ([visibleAnnotationsInBucket containsObject:annotation]) {
CLLocationCoordinate2D actualCoordinate = annotation.coordinate;
[UIView animateWithDuration:0.3 animations:^{
annotation.coordinate = annotation.clusterAnnotation.coordinate;
} completion:^(BOOL finished) {
annotation.coordinate = actualCoordinate;
[self.mapView removeAnnotation:annotation];
}];
}
}
}
gridMapRect.origin.x += gridSize;
}
gridMapRect.origin.y += gridSize;
}
}
- (id<MKAnnotation>)annotationInGrid:(MKMapRect)gridMapRect usingAnnotations:(NSSet *)annotations {
NSSet *visibleAnnotationsInBucket = [self.mapView annotationsInMapRect:gridMapRect];
NSSet *annotationsForGridSet = [annotations objectsPassingTest:^BOOL(id obj, BOOL *stop) {
BOOL returnValue = ([visibleAnnotationsInBucket containsObject:obj]);
if (returnValue) {
*stop = YES;
}
return returnValue;
}];
if (annotationsForGridSet.count != 0) {
return [annotationsForGridSet anyObject];
}
MKMapPoint centerMapPoint = MKMapPointMake(MKMapRectGetMinX(gridMapRect), MKMapRectGetMidY(gridMapRect));
NSArray *sortedAnnotations = [[annotations allObjects] sortedArrayUsingComparator:^(id obj1, id obj2) {
MKMapPoint mapPoint1 = MKMapPointForCoordinate(((id<MKAnnotation>)obj1).coordinate);
MKMapPoint mapPoint2 = MKMapPointForCoordinate(((id<MKAnnotation>)obj2).coordinate);
CLLocationDistance distance1 = MKMetersBetweenMapPoints(mapPoint1, centerMapPoint);
CLLocationDistance distance2 = MKMetersBetweenMapPoints(mapPoint2, centerMapPoint);
if (distance1 < distance2) {
return NSOrderedAscending;
}
else if (distance1 > distance2) {
return NSOrderedDescending;
}
return NSOrderedSame;
}];
return [sortedAnnotations objectAtIndex:0];
}

Resources