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];
Related
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.
I have a lot of pin annotations on the MKMapView in my app, the iPhone gets very slow and unresponsive when a lot of them are in view on the map. I would like the user to be able to zoom, but not out past a certain level, such as 2km squared or something.
Here's what I've got:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[[BicycleLDNService sharedService] requestLocationForClient:self];
CLLocationCoordinate2D zoomLocation;
CLLocation *deviceLocation = [[BicycleLDNService sharedService] deviceLocation];
zoomLocation.latitude = deviceLocation.coordinate.latitude;
zoomLocation.longitude = deviceLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*kMetresPerKilometre, 0.5*kMetresPerKilometre);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.zoomEnabled = YES;
}
Is there some sort of property or delegate method I can employ? Couldn't find anything useful on google or here!
Thanks in advance!
The span defines how much of the map at the given point should be visible and is also how you set the zoom level.
You can access this by using
region.span.latitute=0.5;
region.span.longitude=0.6;
Check the zoom level of the map and then set the zoomEnabled Property NO.
mapView.zoomEnabled=NO;
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];
}
I have a MKCoordinateregion called region. I want to change span of this region without changing it's center. I am using following method to change zoom.
-(void) changeZoom
{
NSLog(#"before zoom span, center are %f, %f,%f,%f", region.span.latitudeDelta, region.span.longitudeDelta, mymap.centerCoordinate.latitude, mymap.centerCoordinate.longitude);
region.span.latitudeDelta = current_factor ;
region.span.longitudeDelta = current_factor ;
region.center.latitude = mymap.centerCoordinate.latitude ;
region.center.longitude = mymap.centerCoordinate.longitude ;
[mymap setRegion:region animated:TRUE];
NSLog(#"after zoom span, center are %f, %f,%f,%f", region.span.latitudeDelta, region.span.longitudeDelta, mymap.centerCoordinate.latitude, mymap.centerCoordinate.longitude);
}
Log shows :
before zoom span, center are 0.021950, 0.021950,19.068080,72.838111
after zoom span, center are 0.043901, 0.043901,19.068040,72.838154
This method set's span accurately as desired.But I dont want any change in the center's coordinate, Because when I zoom out and then zoom back I end up at a location different from starting location.
Is there any method so that zoom in and out are possible without any change in center?
Thanks for any help in advance.
Your before and after center coordinates are almost exactly the same, so it shouldn't be a problem. If you really want the exact same center coordinates, call setCenterCoordinate:animated: right after you use setRegion:animated: and pass some saved center coordinates.
CLLocationCoordinate2D originalCenter = mymap.centerCoordinate;
// ... adjust region
[mymap setCenterCoordinate:originalCenter animated:NO];
Also, you should use YES instead of TRUE for booleans:
[mymap setRegion:region animated:YES];
You need to adjust the region of the map to make sure the aspect ratio fits the frame size of the map view.
Once the region with the desired center is calculated, adjust the region as,
MKCoordinateRegion adjustedRegion = [mymap regionThatFits:region];
[mymap setRegion:adjustedRegion animated:TRUE];
See docs.
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).