I have an array with polygons created from a data file with coordinates per polygon.
So when I plot them on my map I use:
[mapView addOverlays:polygonArray];
and in my viewForOverlay:
if ([overlay isKindOfClass:[MKPolygon class]]) {
MKPolygonView *polyView = [[MKPolygonView alloc] initWithPolygon:overlay];
polyView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
polyView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.1];
polyView.lineWidth = 1;
return polyView;
}
else {
return nil;
}
The problem is that regarding my "colorWithAlphaComponent" the code seems to reuse and recreate the polyView for each Polygon. Therefore the first one is with alpha 0.1 but the second is 2x and so on.. So the last few Polygons aren't "seethrough" anymore.
Here's how it looks:
Based on the problem description and picture, it sounds like you are adding the same polygon multiple times so it gets overlapped with itself.
When overlays overlap, the map view blends their colors together resulting in a darker appearance.
If polygonArray contains unique polygons itself but addOverlays is called multiple times, you should call removeOverlays before addOverlays if the existing polygons on the map are already included in polygonArray.
Another possibility is that polygonArray itself contains duplicate polygons.
Even if addOverlays is called only once, the map will add multiple instances of the same polygon resulting in those overlays overlapping themselves giving them a darker color than expected.
To fix this, you should eliminate the duplication in polygonArray.
Related
I have an image shown below:
In which i have to select all parts one by one and change their colour, I have tried many methods but couldn't able to solve this. I have tried Bezier Path but no success.
This is the code am using currently for selection of any part based on colour, and then i have changed that colour to red as shown below:
But the problem is I have to change colour of only that part of image which i have selected not all which have same colour.
Any idea how to solve this.
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:_bodyImgView]; //where image was tapped
NSLog(#"X = %f \n Y= %f",point.x,point.y);
pickedColor = [self.view colorOfPoint:point image:_bodyImgView.image];
NSString *col = [NSString stringWithFormat:#"%#",pickedColor];
NSLog(#"color %#",pickedColor);
if (![col isEqualToString:#"UIExtendedSRGBColorSpace 0 0 0 0"]) {
UIImage *img = [self.view replaceColor:pickedColor inImage:_bodyImgView.image
withTolerance:25.0];
[_bodyImgView setImage:img];
}
}
colorOfPoint:image is a method to extract colour from picked point.
replaceColor:inImage:withTolerance: is the method to replace the picked color.
Do you need to be able to cope with any image, or is this a fixed image or one of a few you can preprocess? Ideally, you'd create several images, each containing one selectable region only (and the rest transparent). Then it's a matter of just looping over the images and finding one where the corresponding point is opaque, and filling the image in a blend mode that preserves transparency.
If that is not an option, maybe you could perform a seed fill to generate a mask (start searching from the click position and take any color until you hit a white pixel), then apply that mask and then perform the blend mode painting, only touching the white pixels.
Alternately, if you're manually walking the picture to seed fill already, you could look at each pixel and manually apply the color to it, maybe by determining how much blue it contains and then generating a pixel that has that amount of blue instead.
I am a beginner programmer and this is my first app(I am still learning). I have overlaid a polygon onto a map view. I have set its fill color to an image because I'm trying to match an image to a satellite picture. I want to rotate it so that the polygon contents match the map. Is it possible to rotate the image? If not, is there an easier way to overlay an image onto a map view that I could use.
Here is my code:
-(MKOverlayView*)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay {
MKPolygonView *polyView = [[MKPolygonView alloc] initWithOverlay:overlay];
polyView.strokeColor = [UIColor whiteColor];
polyView.fillColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Campus-map labels.jpg"]];
return polyView;
}
Here's what I'm trying to do, if it helps:
http://i.stack.imgur.com/x53HU.jpg
The road which is circled in red should match up. I know that the polygon isn't in the right position -- this is to illustrate how the polygon needs to be rotated.
You can modify the transform property of the polyView object. For example:
polyView.transform = CGAffineTransformMakeRotation(M_PI_4);
will rotate the polygon by pi/4 radians (45 degrees), in a clockwise direction.
You might need to change the polygon's center property to get the effect you want. The center property determines the center of rotation around which the transform rotation takes place.
I am drawing x amount of lines on an MKMapView. The data is being downloaded from a webservice and for each line that needs drawing I am creating a MKPolyline, adding it to an overlayArray (some overlays have more than one polyline) and finally adding that overlay to the map via:
[self.mapView addOverlays:overlayArray];`
The next function is therefore called which works wonderfully:
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if (![overlay isKindOfClass:[MKPolygon class]]) {
MKPolyline *route = overlay;
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:route];
renderer.strokeColor = [lineColours objectForKey:route.title];
renderer.lineWidth = 3.0;
return renderer;
} else {
return nil;
}
}
As you can see, the strokeColor gets the correct UIColor from a pre-defined NSMutableDictionary. This all works as expected.
However, some of the lines overlap and using a single colour is not always desired. I was wondering if there was a way of creating a line made up of two or even 3 colours, but not a gradient along the line as seen in many fitness apps, but colours across the line. For example, another use would be to draw a motorway route made up of two white lines with a transparent strip in the middle.
This is quite important for an app I am developing so I will post any findings I find here, and I hope that other people can share their thoughts and knowledge too.
I am designing a transit app that overlays several routes in the form of MKPolyLines on a map. Currently the colors of the various routes are set as the title property of MKPolyLine. I'm wondering if there is a way to detect when lines of different colors overlap, and then change the color. Currently when two routes are added on top of each other, the color is simply the last one added.
My attempt at the pseudocode
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolyline *polyline = (MKPolyline *)overlay;
UIColor *color = [self colorWithHexString:polyline.title];
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
if the polyline matches an already existing polyline{
color = new color
}
polylineView.strokeColor = color;
polylineView.lineWidth = 5.0;
}
Seems simple enough? Not sure if it is possible to compare polylines and see if one is already on the map, might not be an exact enough identifier. Thanks for your help.
I ended up using the lineDashPattern property of MKPolylineView. Applying this to one of the overlapping lines achieves the desired effect.
I'm using the MapBox SDK with an offline map. I'm adding a RMPath overlay to the mapView and everything is shown ok.
problem no. 1:
while scrolling the map the RMPath overlay is scrolled also, but sometimes it is drawn with an offset (a location where it was just a moment ago) for just a fraction of a second, after that it gets to it's normal place , and this creates a sensation of flickering. Why this happens and how can I get rid of it?
problem no. 2:
while scrolling the map the RMMarker and RMPath overlay "vibrates", it's like the overlay tries to "catch up" with it's normal position when the map is scrolled. It's just a few pixels, but when zoomed it looks pretty bad. this happens most probably because the -draw() method is called only when the map is moved more than just a pixel. How can I make the overlays scroll smoother?
My searches resulted with absolutely nothing, so any help is welcomed.
p.s. tested on iPhone3GS and iPhone4S, same problems on both.
RMPath is deprecated, try using RMShape instead.
Also do not forget to set the bounding box of your annotation before adding it to the map (setBoundingBoxFromLocations can be useful).
Example :
pathAnnotation = [[RMAnnotation alloc]initWithMapView:mapView coordinate:CLLocationCoordinate2DMake(long,lat) andTitle:#"path"];
[pathAnnotation setBoundingBoxFromLocations:pathLocations];
and then in your layerForAnnotation() :
RMShape *path = [[RMShape alloc] initWithView:mapView] ;
[path setLineColor:[UIColor colorWithRed:0.2 green:0.7 blue:1 alpha:0.7]];
[path setLineWidth:4];
// Create real coordinates from data
for(int idx = 0; idx < pathPoints.count; idx++)
{
CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(pathPoints[idx].latitude,pathPoints[idx].longitude);
// First point
if( idx == 0){
[path moveToCoordinate:coord];
}else{
[path addLineToCoordinate:coord];
}
}
return path;
For your problem n°2, I think this could be relative to this issue on MapBox ios sdk
For problem n°1, I have noticed the same behavior, but only when annotations are added while the map is scrolling/zooming. After the map stabilized, the annotations go to the right position. I am still investigating for that.