Auto scroll mkmapview when dragging pin - iphone

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];

Related

How can I Resize the MKCircleView?

I am Trying to Resize MkCircle Through finger touch. How can I start this? can any one suggest me?
Thanks for advance.
I draw the custom circle above the map view with same radius look like circle.
Finding edge point of MKcirlce
convert center point CLLocationCoordinate2D to MKMapPoint
MKMapPoint centerMapPoint = MKMapPointForCoordinate(centerCoordinate)
Calculate MKMapSize of the circle and get width
double circleMapSize = mkCircle.boundingMapRect.size.width
3.create a point on the perimeter of the circle.
MKMapPoint leftMostPoint = MKMapPointMake(centerMapPoint.x - circleMapSize/2.0, centerMapPoint.y)
Now get CLLocationCoordinate2D point from leftMostPoint
CLLocationCoordinate2D leftMostCoordinateOnCircle = MKCoordinateForMapPoint(leftMostPoint)
So now i have center point and edge point then convert it to (x,y) of ios coordinates.
CGPoint myPoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
CGPoint edgePoint = [self.MapView convertCoordinate:originalCoordinate toPointToView:self.MapView];
then i calculate distance b/w those two points using pythagoras theorem
int distnce1=sqrt(powf(edgePoint.x- myPoint.x, 2) + powf(edgePoint.y-myPoint.y, 2));
int finalWidth=2*distnce1;
Then i set distance to custom circle width and height
after pich Gesture end again i try to get edge lattitude and longitude of custom circle by using circle maths
float x = circleView.frame.size.width/2 * sin(0)+circleView.center.x;
float y = circleView.frame.size.width/2 * cos(0)+circleView.center.y;
convert x,y into coordinates
CLLocationCoordinate2D select_coordinate = [self.MapView convertPoint:point toCoordinateFromView:self.view];
then i find distance b/w new coordinates and center point of MKCircle then change the radius of MKCirlce equal to Distance.
One approach which might be simplest is to actually draw the circle yourself above the map view, on a separate transparent view which responds to touch events. Then, when drawing is complete, hide that view but add an actual MKCircle to the map view with the same graphical settings.
Typically it's easier to handling drawing/gestures outside of the map view for things like this.

In iOS, how to trace the swiped distance in google maps?

I've an application where I'm using Google maps SDK.
If the user is doing a wild swipe on the map view controller,(say 100miles), Then I want to take the user back to his current location.
How to implement this functionality in maps ?
How to trace the wild swipe which moved the map 100miles on the screen ?
How to know the intensity of the swipe? and how many miles that swipe take you from the current location based on the zoom level?
Thanks in advance!
What do you mean by wild swipe? Move map 100 miles in one swipe? If so, you can add an UIPanGestureRecognizer to the map view,
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:aGMSMapView action:#selector(pan:)];
[aGMSMapView addGestureRecognizer:panRecognizer];
and implement a method to detect if the state of pan gesture is end or not. If so, check the center of the map view to determine how far you moved
-(void)pan:(UIPanGestureRecognizer*) gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
//get the center of map view, and calculate distance
CGPoint *point = aGMSMapView.center;
CLLocationCoordinate2D coor = [aGMSMapView.projection coordinateForPoint:point];
CLLocation *mapCenter = [[CLLocation alloc]initWithLatitude:coor.latitude longitude:coor.longitude];
CLLocationDistance d = [mapCenter distanceFromLocation:currentLocation];
if (d>= 106900)
{
//move your map view and do whatever you want...
}
}
}

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.

Move MKMapView point to pixel coordinates

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).

Rotating only the MapView's content

I am trying to rotate a map view in my app according the the user's current route. (I don't like to use the built in compasse because only 3GS uses it and it suffers too much interference from other machines, i.e. your own car.).
the CGAffineTransformMakeRotation method will rotate the Whole Map View, so the Google Logo will not be in the lower right of the screen anymore. Also all Annotation Views will rotate and look weird on the App.
Does anyone knows how to rotate just the content (streets drwaings) of the MkMapView?
Thanks
Here is how I'm rotating the MapView (see Rotate MapView using Compass orientation):
[self.mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];
Then to keep the orientation of the annotation views with the top of the device:
for (MKAnnotation *annotation in self.mapView.annotations) {
MKAnnotationView *annotationView = [self.mapView viewForAnnotation:annotation];
[annotationView setTransform:CGAffineTransformMakeRotation(currentHeading.magneticHeading * 3.14159 / 180)];
}
If you want the point of rotation of the annotationViews to be say the bottom center, I believe you would set each view's anchorPoint as follows:
annotationView.layer.anchorPoint = CGPointMake(0.5, 1.0);
I'm still having some issues with the annotationView position changing when I do this though (probably same problem as stackoverflow question: changing-my-calayers-anchorpoint-moves-the-view).