Check if MKPolygon is inside visible rect or another MKPolygon - ios

I have a MKMapview with lot of polygons added as overlays, To optimize the memory I need to know before adding an overlay if the polygon is inside visible area of the MKMapview
I can even create a whole visible area polygon with mapview all corner coordinates, for example topLeft as below
func topLeftCoordinate() -> CLLocationCoordinate2D {
return convert(bounds.origin, toCoordinateFrom: self)
}
with all the corner coordinates, I can create a current_visible_area_polygon and I want to check the polygons I add is inside this current_visible_area_polygon.
so it comes down to two questions
Is it possible to check if a polygon is inside another polygon or atleast intersects OR
if a polygon is inside visible maprect

I found the answer to be the following
let mapView: MKMapView
let mkPolygon: MKPolygon
mapView.visibleMapRect.isIntersects(mkPolygon)

Related

How to put iOS Magnifying Glass effect on Google Maps (GMSMapKit)

How to put the UITextView texting editing Magnifying Glass on the Google Maps GMSMapView
For example I have a GMSMapView to show my current location
I want to trigger a overlay Magnifying Glass view when calling delegate methods
mapView:didChangeCameraPosition: and mapView:willMove: in GMSMapViewDelegate
The purpose is to provide an overlay zooming subView according to the user tapping coordinates (like github.com/acoomans/iOS-MagnifyingGlass did on ImageView)
Please let me know if this is possible for Google Maps for iOS or if iOS MapKit can support this kind of customization
Update #2: mapView.addSubView(mapSubView) works now. But it pollutes the Main GMSMapView
Update #1: I tried mapView.addSubView, it seems does not work for GMSMapView although inherited from UIView
The intention of below code snippet is to retrieve user's touch point at the map and converts it into CGPoint for creating a second GMSMapView
func mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
println("Tapping at (\(coordinate.latitude), \(coordinate.longitude))")
// 1. retrieve the user touch position as CLLocationCoordinate2D
let cameraPosition = GMSCameraPosition.cameraWithLatitude(coordinate.latitude, longitude: coordinate.longitude, zoom: 20)
// 2. convert it into CGPoint
let screenTouchPoints = mapView.projection.pointForCoordinate(coordinate)
// 3. set the CGRect for init the mapSubView
let frame = CGRectMake(screenTouchPoints.x, screenTouchPoints.y, 100, 100)
mapSubView = GMSMapView.mapWithFrame(frame, camera: cameraPosition)
// 4. Finally add to the main Map View
mapView.addSubview(mapSubView)
}
It seems plausible with a GMSMapView. Maybe have a second GMSMapView on top of the original(high corner radius to create a circle?) and animate the alpha and scale along with the zoom level within the second map. Do this whenever mapView:didChangeCameraPosition etc is called.
By taking reference to this Github Project, GMSMapView can also be magnified by putting the view hierarchy in this way:
View Controller > Magnifying View > The View want to be zoomed
The core rendering mechanism is
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, self.frame.size.width/2, self.frame.size.height/2 );
CGContextScaleCTM(context, scale, scale);
CGContextTranslateCTM(context, -touchPoint.x, -touchPoint.y + (self.scaleAtTouchPoint? 0 : self.bounds.size.height/2));
[self.viewToMagnify.layer renderInContext:context];
}
loupe = ACLoupe()
magnifyingGlass = ACMagnifyingGlass()
magnifyingView = ACMagnifyingView(frame: self.view.frame)
magnifyingView.magnifyingGlass = loupe
magnifyingView.addSubview(Your_Subview_here)
by this method the magnifying view can capture current frame context, because Magnifying View is the container of other views so that the capturing can show the current UI situation and zoom by 1.5 times (default scale factor)

MapView - change map center point

I have this view with a map that covers most of the screen's area.
On top of the map, I have some images, buttons, text boxes. They're located in the top portion of the map, which I'm calling here "useless map", while the bottom part is for actually viewing the map. This is the "useful map".
By default, the map property centerCoordinate is the center with respect to the full map, as seen as the red circle in the image:
The useful map is marked in green, and I wanted the green circle to be used as a center point.
I can center the map or add a pin, for instance, in that precise point by adding or subtracting from the coordinate's latitude, but the actual value I need to add depends on the zoom level.
Is it possible to change the map center point, or make any changes that can make the map act as if that point (the green circle) is the center?
The purpose of this is that I want to drop a pin at (or move the current pin to) the pseudo-center point (the green one) every time the user drags the map. So to do this, I'd need to get the centerCoordinate (the red one) and then add an offset.
You can add layout margins for the map.
extension MKMapView {
/*
* This is to hide the legal link on the map.
*/
public func centerMapWithBottom(bottomPadding: CGFloat) {
self.layoutMargins = UIEdgeInsets(top: 0, left: 0, bottom: bottomPadding, right: 0)
}
}
This will shift the map center up. Similarly, it can be done for other scenarios as well.
Instead of showing user location for the map just drop an annotation pin at
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = myCoordinate; //CLLocationCoordinate2D myCoordinate
[self.mapView addAnnotation:point];

Moving an MKMapView to a specific location relative to the superview

I have a few annotations added to a MKMapView, and when the user clicks on one of the annotations, it displays a UICalloutView with a right accessory button which adds a UIView to the map, displaying some information about that specific location. This UIView is centred in the superview of the map, and in order to show that the information in that view is relative to the annotation, I would like to shift the visible map rect down (on the y axis), and center it on the x axis so that the annotation is directly under the view.
I am doing the following to centre the annotation, however, I don't know how to move the annotation down on the y axis so that it sits under the added UIView. Please can you tell me how I can do so?
[self.mapView setCenterCoordinate:[annotation coordinate] animated:YES];
If you want to shift the map down so it's centered on a particular coordinate, but shift it down, say, 40%, so you have space for something above it, you could do something like the following:
CLLocationCoordinate2D center = coordinate;
center.latitude -= self.mapView.region.span.latitudeDelta * 0.40;
[self.mapView setCenterCoordinate:center animated:YES];
You can get the size of the information view, then you know how much you want to use the map (based on the difference between its size and the map view size). Now you know the offset, you can calculate the point (in the view coordinate system) that should be moved to the centre so that the annotation is moved down). Then you can use convertPoint:toCoordinateFromView: to find the coordinate for that point to use with setCenterCoordinate:animated:.

MKMapView centerLocation

In my application there is an MKMapView and I am trying to get the center coordinates of the map region that is currently visible. I am using following method so that if user moves the visible region I'll get new center coordinates.
- (void)mapView:(MKMapView *)mapView1 regionDidChangeAnimated:(BOOL)animated
{
CLLocationCoordinate2D centre = [mapView centerCoordinate];
NSLog(#"MAP CENTER = %f,%f",centre.latitude,centre.longitude);
}
the problem is that when I switch to the UIViewController that contains MKMapView it gives MAP CENTER = 0.000000,0.000000 for two times then gives the actual coordinates MAP CENTER = 55.755786,37.617633. I want the actual coordinates as soon as I switch to that UIViewController.
Is the coordinates (55.755786,37.617633) your current location ?
MKMapView takes some time to get a lock on GPS to fetch the coordinates for your current location. Until then centerCoordinate might return (0,0)
Try this this may help you.
self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

Convert MKPolygon to UIView

I have a MKMapView with a Polygon overlay that I need to convert into a UIView. I have the reference to the MKMapView and the MKPolygon, but I can't seem to find a way to pull the coordinates of the MKPolygon and convert them back into screen coordinates for the UIView.
You can convert polygon points to view coordinates (if that is what you need) like this:
iPhone SDK: Convert MKMapPoint to CGPoint

Resources