lundi 2 mars 2015

Zoom in/zoom out to always show 2 annotations in Mapview iOS

I have 1 annotation (the store) able to drag and drop by changing the radius and the blue dot (user location) on the mapview.


When changing the radius of the store (annotation) how to the mapview always show that annotation and the current location (blue dot) on the map? I have already implemented the zoom in/ zoom out when changing the radius but it seems to not correct. When the radius is big, the mapview should zoom out to show store (annotation) and the current user location. Can you help me to correct this code? Thanks much.


Here is my code:



-(void) displayCurrentLocationAndPin
{
float latitude = self.locationManager.location.coordinate.latitude;
float longitude = self.locationManager.location.coordinate.longitude;

CLLocationCoordinate2D SouthWest = CLLocationCoordinate2DMake(latitude, longitude);
CLLocationCoordinate2D NorthEast = droppedAt;

NorthEast.latitude = MAX(NorthEast.latitude, droppedAt.latitude);
NorthEast.longitude = MAX(NorthEast.longitude, droppedAt.longitude);

SouthWest.latitude = MIN(SouthWest.latitude, latitude);
SouthWest.longitude = MIN(SouthWest.longitude, longitude);

MKCoordinateRegion region;

region.span.latitudeDelta = fabs(NorthEast.latitude - SouthWest.latitude) * 1.2;
region.span.longitudeDelta = fabs(SouthWest.longitude - NorthEast.longitude) * 1.2;

region.center.latitude = NorthEast.latitude - (NorthEast.latitude - SouthWest.latitude) * 0.5;
region.center.longitude = NorthEast.longitude + (SouthWest.longitude - NorthEast.longitude) * 0.5;

MKCoordinateRegion savedRegion = [_mapView regionThatFits:region];

[_mapView setRegion:savedRegion animated:YES];

[self autoZoomMapView];

}



-(void) autoZoomMapView
{

MKMapPoint mapPoint = MKMapPointForCoordinate(droppedAt);
lastPoint = mapPoint;

oldoffset = [circleView getCircleRadius];

MKMapRect mRect = self.mapView.visibleMapRect;
MKMapRect circleRect = [circleView circlebounds];


/* Check if the map needs to zoom */
if(circleRect.size.width > mRect.size.width * .55){


NSLog(@"HERE NO.1");

MKCoordinateRegion region;
//Set Zoom level using Span
MKCoordinateSpan span;
region.center=droppedAt;

span.latitudeDelta=self.mapView.region.span.latitudeDelta * 2.0;

span.longitudeDelta=self.mapView.region.span.longitudeDelta * 2.0;

region.span=span;
[_mapView setRegion:region animated:TRUE];
}

if(circleRect.size.width < mRect.size.width *1.25){

NSLog(@"HERE NO.2");



MKCoordinateRegion region;
//Set Zoom level using Span
MKCoordinateSpan span;
region.center=droppedAt;
span.latitudeDelta=_mapView.region.span.latitudeDelta /3.0002;
span.longitudeDelta=_mapView.region.span.longitudeDelta /3.0002;
region.span=span;
[_mapView setRegion:region animated:TRUE];
}


double meterDistance = (mapPoint.x - lastPoint.x)/MKMapPointsPerMeterAtLatitude(self.mapView.centerCoordinate.latitude)+oldoffset;

NSLog(@"meterDistance %f",meterDistance);

if(meterDistance > 0){
if (CurrentmeterDistance != meterDistance) {

NSLog(@"redraw Circle");

CurrentmeterDistance = meterDistance;

[circleView setCircleRadius:CurrentmeterDistance];
}

}

}



Aucun commentaire:

Enregistrer un commentaire