Move MKMapView point to pixel coordinates - iphone

I have a quick question. I am using a custom view as a callout accessory for my map view. I am having some issues with getting the annotation to move to the right bottom corner of the said view. I am currently trying to get the CGPoint coords of the selected annotation, but beyond that I've drawn a blank Any help would be greatly appreciated.
The current code I'm using (I know it is incorrect is:)
CGPoint bottomLeftPoint = CGPointMake(xOffset,[self.internalAnnotationCallout view].frame.size.height);
CLLocationCoordinate2D bottomLeftCoord = [self.branchLocation convertPoint:bottomLeftPoint toCoordinateFromView:self.branchLocation];
MKCoordinateSpan span = {latitudeDelta: kMapMultiLatDelta, longitudeDelta: kMapMultiLatDelta};
MKCoordinateRegion region = {bottomLeftCoord, span};
[self.branchLocation setRegion:region animated:YES];
//[self.branchLocation setCenterCoordinate:newCenterCoordinate animated:YES];

EDIT:
Ok so I messed around with this a little and was able to to put something together that seems to work, hoping that I now actually understand what you're trying to achieve!
- (void)shiftToCorner{
//ignore "Annotation", this is just my own custom MKAnnotation subclass
Annotation *currentAnnotation = [[mapView selectedAnnotations] objectAtIndex:0];
[mapView setCenterCoordinate:currentAnnotation.coordinate];
CGPoint fakecenter = CGPointMake(20, 20);
CLLocationCoordinate2D coordinate = [mapView convertPoint:fakecenter toCoordinateFromView:mapView];
[mapView setCenterCoordinate:coordinate animated:YES];
}
Let me quickly explain what this does; Let's say you want your annotation to move to a position 20 points in from the right edge and 20 points up from the bottom edge of the map view. If you think about it, if the current annotation is at the center of the map view, then the distance to this point is the same as the distance to (20, 20). This means that we can send our annotation to this point by first centering our map view on the annotation, then animating to (20, 20).

Related

Auto scroll mkmapview when dragging pin

Is there any good code which implements auto scrolling of MKMapView when dragging pin?
The effect I'm trying to achieve is a map scrolling when I drag the pin and reach edges of the map. When I move pin out of the edges I expect scrolling to stop and when I drop it, map shell move until pin reaches center of the screen.
I know how to center map on chosen location, but I'm not really have an idea how to scroll it while dragging pin.
It would really help if someone could just direct me to a logic of the way how to implement it.
MKMapRect mapRect = [self.mapView visibleMapRect];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(42.777126,-76.113281);
MKMapPoint point = MKMapPointForCoordinate(coordinate);
mapRect.origin.x = point.x - mapRect.size.width * 0.3;
mapRect.origin.y = point.y - mapRect.size.height * 0.70;
[self.mapView setVisibleMapRect:mapRect animated:YES];

How to Get area of visible map in Iphone

I have set map view successfully in iPhone with current location with maximize zoom.
like,
myMapView.showsUserLocation = YES;
.....
span.latitudeDelta=0.0001;
span.longitudeDelta=0.0001;
...
Now i want to get complete area of visible portion of Map from its center point in any of measure like km,m,number etc.
any help..
Its really easy to do that:
MKMapRect visibleRect = [myMapView visibleMapRect];
Then there are some functions to measure the rectangle in meters such as:
CLLocationDistance MKMetersBetweenMapPoints(
MKMapPoint a,
MKMapPoint b
);
Take a look at Documentation and Sample Code
And try something like this:
MKCoordinateRegion yourRegion;
// Take a look at Lisbon Downtown
yourRegion.center = CLLocationCoordinate2DMake(38.715, -9.140);
yourRegion.span = MKCoordinateSpanMake(0.02, 0.02);
[self.mapView setRegion:yourRegion animated:YES];

Black Translucent Navigation Bar + MKMapView -> Setting map center is not visually correct

I have a Map View in a Navigation Controller under translucent black navigation, status and search and toolbars.
The actual height of the map includes the area below these bars.
The + left accessory button makes the map center on the Pin.
The left image is before the map is centered to the pin's coordinates.
The right one is when the map in centered to the pin's coordinates.
The problem is that the center should be the center of the visible map area and not the whole map area. (which causes the pin to not appear in the center of the map.
Is there some offset or boundary setting I can do so the Map View will center correctly?
You can overlay a transparent UIView (tView) for your visible map area (below navbar), get his point, calculate offset you need and setRegion again. (I guess you're using setRegion to center the map):
CGPoint currentPoint = [mapView convertCoordinate:myPin.coordinate toPointToView:self.view];
CGRect tFrame = [tView frame];
tFrame.origin.y = currentPoint.y - tFrame.size.height;
tFrame.origin.x = currentPoint.x - (tFrame.size.width/2);
MKCoordinateRegion newRegion = [mapView convertRect:tFrame toRegionFromView:self.view];
[mapView setRegion:newRegion animated:YES];
You should double check the tFrame so you can set it with the best values for your application.

Showing Specific Region Using MapKit

I want to know is it possible to show only specific region on map not the full world map using Map Kit.
Like if i want to show Asia map in my application then map kit hides remaining part of the map.
To handle the "map kit hides remaining part of the map" requirement, one thing you can do is create a black polygon overlay that covers the whole world with a cutout over Asia (or wherever you like).
For example, where you initialize the map (eg. in viewDidLoad):
CLLocationCoordinate2D asiaCoords[4]
= { {55,60}, {55,150}, {0,150}, {0,60} };
//change or add coordinates (and update count below) as needed
self.asiaOverlay = [MKPolygon polygonWithCoordinates:asiaCoords count:4];
CLLocationCoordinate2D worldCoords[4]
= { {90,-180}, {90,180}, {-90,180}, {-90,-180} };
MKPolygon *worldOverlay
= [MKPolygon polygonWithCoordinates:worldCoords
count:4
interiorPolygons:[NSArray arrayWithObject:asiaOverlay]];
//the array can have more than one "cutout" if needed
[myMapView addOverlay:worldOverlay];
and implement the viewForOverlay delegate method:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
pv.fillColor = [UIColor blackColor];
pv.alpha = 1.0;
return pv;
}
return nil;
}
This looks like this:
If you also want to restrict the user from scrolling beyond Asia or zooming too far out, then you'll need to do that manually as well. One possible way is described in Restrict MKMapView scrolling. Replace theOverlay in that answer with asiaOverlay.
You can specify the region as an MKCoordinateRegion and then tell an MKMapView instance to only show that region using the setRegion and regionThatFits message.
Alternatively you could use the visibleMapRect property instead of the region. This might better fit your needs.
In short read the MKMapView Class Reference document from Apple.
Lifting from some code I've done in the past that assumes a mapView and a given location called locationToShow I used an MKCoordinateRegion.
- (void) goToLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
region.center=locationToShow;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}

OpenGL Ortho Coordinates

I have an OpenGL view overlaid on top of an MKMapView. My goal is to have the coordinates of the glView to have the same values of the MKMapPoints on the map. To do this I found the values for the MKMapPoint on the top left and the bottom right of the map.
CLLocationCoordinate2D coordinateTopLeft = [mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:mapView];
MKMapPoint pointTopLeft = MKMapPointForCoordinate(coordinateTopLeft);
CLLocationCoordinate2D coordinateBottomRight = [mapView convertPoint:CGPointMake(mapView.frame.size.width, mapView.frame.size.width) toCoordinateFromView:mapView];
MKMapPoint pointBottomRight = MKMapPointForCoordinate(coordinateBottomRight);
Then I set the right, left, bottom, and top coordinates of the glView to match these points accordingly.
glOrthof(pointTopLeft.x, pointBottomRight.x, pointBottomRight.y, pointTopLeft.y, -1, 1);
Unfortunately, this did not work. The triangles that I tried to draw did not work. I tested and the points that I was trying to draw were located in the correct domain.
Am I just misunderstanding how the glOrthof method works?
Have you tried flipping pointBottomRight.y and pointTopLeft.y? The OpenGL y-coordinate system is inverted from most commonly used conventions.