When I show UIViewController with UIMapView, in viewDidLoad I add PointAnnotation to map and to array. Then I calculate the region for these annotations and set that region to map.
The point is that sometimes (basically only once after installing app for the first time) the map shows region for equator even if my data is correct.
-(void)viewDidLoad
{
NSLog(#"Spot: %#", _spot);
_annotations = [[NSMutableArray alloc] init];
[super viewDidLoad];
// Adding annotation of spot
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = self.spot.latitude;
annotationCoord.longitude = self.spot.longitude;
POPointAnnotation *annotationPoint = [[POPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = self.spot.name;
annotationPoint.subtitle = self.spot.address;
annotationPoint.type = PointAnnotaitonTypeSpot;
[_annotations addObject:annotationPoint];
[((SpotsMapView *)self.view).mapView addAnnotation:annotationPoint];
[annotationPoint release];
// Adding annotation of user position
CLLocationCoordinate2D userCoord;
userCoord = [[POLocationManager sharedInstance] location].coordinate;
POPointAnnotation *userPoint = [[POPointAnnotation alloc] init];
userPoint.coordinate = userCoord;
userPoint.title = NSLocalizedString(#"Punkt startowy", #"Punkt startowy");
userPoint.type = PointAnnotationTypeStart;
[_annotations addObject:userPoint];
[((SpotsMapView *)self.view).mapView addAnnotation:userPoint];
[userPoint release];
[((SpotsMapView *)self.view).mapView setRegion:[self regionFromLocations] animated:YES];
[((SpotsMapView *)self.view).mapView setShowsUserLocation:YES];
}
and region here:
- (MKCoordinateRegion)regionFromLocations {
CLLocationCoordinate2D upper = [[self.annotations objectAtIndex:1] coordinate];
CLLocationCoordinate2D lower = [[self.annotations objectAtIndex:1] coordinate];
NSLog(#"Loc Upper: %f, %f", upper.latitude, upper.longitude);
NSLog(#"Loc Lower: %f, %f", lower.latitude, lower.longitude);
// FIND LIMITS
for(MKPointAnnotation *eachLocation in self.annotations) {
if([eachLocation coordinate].latitude > upper.latitude) upper.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].latitude < lower.latitude) lower.latitude = [eachLocation coordinate].latitude;
if([eachLocation coordinate].longitude > upper.longitude) upper.longitude = [eachLocation coordinate].longitude;
if([eachLocation coordinate].longitude < lower.longitude) lower.longitude = [eachLocation coordinate].longitude;
}
NSLog(#"Loc Upper: %f, %f", upper.latitude, upper.longitude);
NSLog(#"Loc Lower: %f, %f", lower.latitude, lower.longitude);
// FIND REGION
MKCoordinateSpan locationSpan;
locationSpan.latitudeDelta = upper.latitude - lower.latitude + 0.003;
locationSpan.longitudeDelta = upper.longitude - lower.longitude+ 0.003;
CLLocationCoordinate2D locationCenter;
locationCenter.latitude = (upper.latitude + lower.latitude) / 2;
locationCenter.longitude = (upper.longitude + lower.longitude) / 2;
MKCoordinateRegion region = MKCoordinateRegionMake(locationCenter, locationSpan);
return region;
}
Once after installing app I get this:
and after that I get this view:
Any idea what can be wrong?
Related
I want to set pin in multiple addresses or location in the map and then draw a line among this pin-point. Pins and Line will show in the map . i find a code , that only show pin of some location .. here is my code . .
- (void)viewDidLoad {
[super viewDidLoad];
mapView.delegate=self;
NSMutableArray* annotations=[[NSMutableArray alloc] init];
for (int i =1; i <=10; i++) {
CLLocationCoordinate2D theCoordinate1;
if (i==1) {
theCoordinate1.latitude = 37.786996;
theCoordinate1.longitude = -122.419281;
}
if (i==2) {
theCoordinate1.latitude = 37.810000;
theCoordinate1.longitude = -122.477989;;
}
if (i==3) {
theCoordinate1.latitude = 37.80000;
theCoordinate1.longitude = -122.407989;
}
if (i==4) {
theCoordinate1.latitude = 37.82000;
theCoordinate1.longitude = -122.407989;
}
MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];
myAnnotation1.coordinate=theCoordinate1;
[mapView addAnnotation:myAnnotation1];
[annotations addObject:myAnnotation1];
}
MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(flyTo)) {
flyTo = pointRect;
} else {
flyTo = MKMapRectUnion(flyTo, pointRect);
}
}
mapView.visibleMapRect = flyTo;
}
i want to draw a line among this points of . and finally it looks like a path , and pins are standing on this path or line .
thank you advance .
for example
CLLocationCoordinate2D coord1 = CLLocationCoordinate2DMake(31.484137685, 120.371875243);
CLLocationCoordinate2D coord2 = CLLocationCoordinate2DMake(31.484044745, 120.371879653);
points[0] = MKMapPointForCoordinate(coord1);
points[1] = MKMapPointForCoordinate(coord2);
MKPolyline *line = [MKPolyline polylineWithPoints:points count:2];
[myMap addOverlay: line];
and you shuold add this code below.
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
if ([overlay isKindOfClass:[MKPolyline class]])
{
MKPolylineView *lineview=[[[MKPolylineView alloc] initWithOverlay:overlay] autorelease];
lineview.strokeColor=[[UIColor blueColor] colorWithAlphaComponent:0.5];
lineview.lineWidth=2.0;
[arr release];
return lineview;
}
}
First, thank you, and next, it's my problem, the core code:
for(int idx = 0; idx < route0.polyLine.count; idx++)
{
CLLocationCoordinate2D coordinate = [[route0.polyLine objectAtIndex:idx] coordinate];
minLat = MIN(minLat, coordinate.latitude);
minLon = MIN(minLon, coordinate.longitude);
maxLat = MAX(maxLat, coordinate.latitude);
maxLon = MAX(maxLon, coordinate.longitude);
}
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 1.06f*(maxLat - minLat);
span.longitudeDelta = 1.06f*(maxLon - minLon);
CLLocationCoordinate2D location;
location.latitude = (minLat + maxLat)/2;
location.longitude = (minLon + maxLon)/2;
region.span=span;
region.center=location;
[_mapView regionThatFits:region];
[_mapView setRegion:region animated:false];
My attentio is let the annotations on the mapview just fit screen, but if the annotations is too close, will appear:
This question already has answers here:
Finding Path/Route Between two points on MapKit in iPhone
(5 answers)
Closed 9 years ago.
I am using MKMapView to show route. I want to zoom out this map so that it will show both source at destination at a time.
how can I zoom out map as per distance so that it will show whole route in one screen.
I am using this code:
double maxLatitude = annotation.coordinate.latitude;
double maxLongitude = annotation.coordinate.longitude;
double minLatitude = annotation.coordinate.latitude;
double minLongitude = annotation.coordinate.longitude;
MKCoordinateRegion region;
region.center.latitude = (minLatitude + maxLatitude) / 2;
region.center.longitude = (minLongitude + maxLongitude) / 2;
region.span.latitudeDelta = (maxLatitude - minLatitude) * MAP_PADDING;
region.span.latitudeDelta = (region.span.latitudeDelta < MINIMUM_VISIBLE_LATITUDE)
? MINIMUM_VISIBLE_LATITUDE
: region.span.latitudeDelta;
region.span.longitudeDelta = (maxLongitude - minLongitude) * MAP_PADDING;
[self.mapView addAnnotation:annotation];
[self.mapView regionThatFits:region];
You can try it by the following method
for (int i = 1; i<[arrayOfPointsToFindDistance count]; i++) {
connection_listdata *detail_connection=[arrayOfPointsToFindDistance objectAtIndex:i];
[self findDistancewithConnecionList:detail_connection];
}
findDistancewithConnecionList method is given below.
-(void)findDistancewithConnecionList:(connection_listdata *)connectionlist{
NSLog(#"sssssss");
CLLocation *oldLoc = [[CLLocation alloc] initWithLatitude:connectionlist.coordinate_connection.latitude longitude: connectionlist.coordinate_connection.longitude];
for(int i =0; i<[mapArray count];i++){
NSLog(#"ghsghshhs");
NSMutableArray *firstArrayTemp = [mapArray objectAtIndex:i];
connection_listdata *detail_connection=[firstArrayTemp objectAtIndex:0];
CLLocation *newLoc = [[CLLocation alloc] initWithLatitude:detail_connection.coordinate_connection.latitude longitude: detail_connection.coordinate_connection.longitude];
CLLocationDistance d = [newLoc distanceFromLocation:oldLoc];
NSLog(#"distanceeeee:%f",d);
if(d <= 500){
NSLog(#"foundddd");
NSLog(#"maparray values= %#",mapArray);
[firstArrayTemp addObject:connectionlist];
[mapArray addObject:firstArrayTemp];
return;
}
[newLoc release];
}
NSMutableArray *array = [[NSMutableArray alloc]init];
[array addObject:connectionlist];
[mapArray addObject:array];
[array release];
[oldLoc release];
return;
}
When i drop multiple pins in map depends on different latitude and longitude values. im getting all pins are at one place in map.
I wrote this code for dropping multiple pins .
dealerMapView.mapType = MKMapTypeStandard;
dealerMapView.zoomEnabled =YES;
[dealerMapView setDelegate:nil];
dealerMapView.scrollEnabled = YES;
dealerMapView.showsUserLocation = NO;
CLLocationCoordinate2D location;
for (int i =0; i<[delarsInfoArray count]; i++) {
NSString *lattitudeValue = [[delarsInfoArray objectAtIndex:i]objectForKey:#"LATITUDE" ];
NSString *longitudeValue = [[delarsInfoArray objectAtIndex:i]objectForKey:#"LONGITUDE" ];
CLLocationCoordinate2D pCoordinate ;
pCoordinate.latitude = [lattitudeValue floatValue];
pCoordinate.longitude = [longitudeValue floatValue];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
region.span = span;
region.center = location;
[dealerMapView setRegion:region animated:YES];
myAnnotation1 = [[MyAnnotation alloc] init];
myAnnotation1.coordinate = location;
myAnnotation1.title = [[delarsInfoArray objectAtIndex:i]objectForKey:#"STORE"];
[dealerMapView addAnnotation:myAnnotation1];
}
Try this code:
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta= 0.2;
if( [delarsInfoArray count] > 0 ){
CLLocationCoordinate2D start;
NSString *lattitudeValue = [[delarsInfoArray objectAtIndex:0]objectForKey:#"LATITUDE" ];
NSString *longitudeValue = [[delarsInfoArray objectAtIndex:j]objectForKey:#"LONGITUDE" ];
// create region, consisting of span and location
MKCoordinateRegion mapRegion;
mapRegion.span = span;
mapRegion.center = start;
//Move the map to our location //
[dealerMapView setRegion:mapRegion animated:YES];
}
// do not show pins and annotations more then 200 in the map at a time.
for (int j = 0; j< ([delarsInfoArray count] < 200?[delarsInfoArray count]:200); j++) {
NSString *lattitudeValue = [[delarsInfoArray objectAtIndex:i]objectForKey:#"LATITUDE"];
NSString *longitudeValue = [[delarsInfoArray objectAtIndex:i]objectForKey:#"LONGITUDE" ];
CLLocationCoordinate2D pinLocation;
if(([lattitudeValue floatValue] != 0) && ([longitudeValue floatValue] != 0) ) {
pinLocation.latitude = [lattitudeValue floatValue];
pinLocation.longitude = [longitudeValue floatValue];
if(pinLocation.latitude !=0 && pinLocation.longitude !=0) {
myAnnotation1 = [[MyAnnotation alloc] init];
myAnnotation1.coordinate = location;
myAnnotation1.title = [[delarsInfoArray objectAtIndex:i] objectForKey: #"STORE"];
[dealerMapView addAnnotation:myAnnotation1];
}
}
}
I hope it helps you!! Best Luck. Cheers!!
i have the following code...
DistanceInformation *distanceInformation=[[DistanceInformation alloc]init];
NSArray *latLongArray=[distanceInformation calculateDistance];
[distanceInformation release];
NSLog(#"lat l %#",latLongArray);
NSUInteger length,count;
length = [[latLongArray objectAtIndex:0] count];
//Calcualte center of the map based on current location and airport location
CLLocationCoordinate2D centerOfMap;
centerOfMap.latitude = (27.1766700 + 28.6361600)/2;
centerOfMap.longitude = (78.0080700 + 78.0526500)/2;
//Set map span according to the distance between airport and user location
CGFloat zoomingLevel;
//set the zoom level of the map according to the distance to airport
if([[latLongArray objectAtIndex:2] intValue] <= 50)
zoomingLevel = 0.2;
else if(([[latLongArray objectAtIndex:2] intValue] > 50)&&([[latLongArray objectAtIndex:2] intValue] <= 100))
zoomingLevel = 0.6;
else if(([[latLongArray objectAtIndex:2] intValue]> 100)&&([[latLongArray objectAtIndex:2] intValue] <= 500))
zoomingLevel = 1.7;
else if(([[latLongArray objectAtIndex:2] intValue] > 500)&&([[latLongArray objectAtIndex:2] intValue] <= 1000))
zoomingLevel = 2.0;
else
zoomingLevel = 2.5;
NSLog(#"Center of map: %f, %f",centerOfMap.latitude, centerOfMap.longitude);
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=zoomingLevel;
span.longitudeDelta=zoomingLevel;
region.span = span;
region.center = centerOfMap;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
CLLocationCoordinate2D coords[length];
for (count = 0; count<length; count++) {
NSLog(#"coo ");
coords[count] = CLLocationCoordinate2DMake([[[latLongArray objectAtIndex:0] objectAtIndex:count] doubleValue], [[[latLongArray objectAtIndex:1] objectAtIndex:count] doubleValue]);
}
//Display polyline containing route points as an overlay over the mapview
MKPolyline *polyLine=[MKPolyline polylineWithCoordinates:coords count:length];
NSLog(#"polyLine.pointCount %d",polyLine.pointCount) ;
[mapView addOverlay:polyLine];
[polyLine release];
but the polyline is not visible on map. The polyline is getting initialized with values that I have checked..
Noob mistake on my side... Forgot to implement the MKMapViewDelegate method..
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay >)overlay{
}