Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw Map Path among multiple location

I am new in iphone development, i want to draw path between 5 location how i can draw path in google map. I want path from location1 to location2 , location2 to location3, location3 to location4 and location4 to location5.

here is my code

- (void)viewDidLoad {
[super viewDidLoad];

NSDictionary *d1,*d2,*d3,*d4,*d5;

d1=[NSDictionary dictionaryWithObjectsAndKeys:@"location1",@"comments",@"1",@"id",@"23.02941395",@"latitude",@"72.54620655",@"longitude",nil];
d2=[NSDictionary dictionaryWithObjectsAndKeys:@"location2",@"comments",@"2",@"id",@"23.028359049999995",@"latitude",@"72.54537318333334",@"longitude",nil];
d3=[NSDictionary dictionaryWithObjectsAndKeys:@"location3",@"comments",@"3",@"id",@"23.029545",@"latitude",@"72.546036",@"longitude",nil];
d4=[NSDictionary dictionaryWithObjectsAndKeys:@"location4",@"comments",@"4",@"id",@"23.030050",@"latitude",@"72.546226",@"longitude",nil];
d5=[NSDictionary dictionaryWithObjectsAndKeys:@"location5",@"comments",@"5",@"id",@"23.030050",@"latitude",@"72.546022",@"longitude",nil];


self.reports=[NSArray arrayWithObjects:d1,d2,d3,d4,d5,nil];


for (NSDictionary *d in self.reports) {
    float latitude=[[d valueForKey:@"latitude"] floatValue];
    float longitude=[[d valueForKey:@"longitude"] floatValue];

    Place* home = [[[Place alloc] init] autorelease];
    home.name = [d valueForKey:@"comments"];
    home.latitude = latitude;
    home.longitude = longitude;

    PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];             
    [mapView addAnnotation:from];

}


[self centerMap];

   }

   -(void) centerMap 

   {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 120;
CLLocationDegrees minLon = 150;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];
NSLog(@"%@",temp);
for (int i=0; i<[temp count];i++) {
    Place* home = [[[Place alloc] init] autorelease];
    home.latitude = [[[temp objectAtIndex:i] valueForKey:@"latitude"]floatValue];
    home.longitude =[[[temp objectAtIndex:i] valueForKey:@"longitude"]floatValue];

    PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];     

    CLLocation* currentLocation = (CLLocation*)from ;
    if(currentLocation.coordinate.latitude > maxLat)
        maxLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.latitude < minLat)
        minLat = currentLocation.coordinate.latitude;
    if(currentLocation.coordinate.longitude > maxLon)
        maxLon = currentLocation.coordinate.longitude;
    if(currentLocation.coordinate.longitude < minLon)
        minLon = currentLocation.coordinate.longitude;

    region.center.latitude     = (maxLat + minLat) / 2;
    region.center.longitude    = (maxLon + minLon) / 2;
    region.span.latitudeDelta  =  maxLat - minLat;
    region.span.longitudeDelta = maxLon - minLon;
}
[mapView setRegion:region animated:YES];

    }
     - (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id  <MKAnnotation>)annotation
   {

if (annotation == mapView.userLocation) 
    return nil;

MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];

if (pin == nil)
    pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
else
    pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];

pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
//pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;

   }

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view  calloutAccessoryControlTapped:(UIControl *)control
    {
NSString *strTitle = [NSString stringWithFormat:@"%@",[view.annotation title]];
NSMutableDictionary *d;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];


for (int i = 0; i<[temp count]; i++)
{
    d = (NSMutableDictionary*)[temp objectAtIndex:i];
    NSString *strAddress = [NSString stringWithFormat:@"%@",[d valueForKey:@"comments"]];

    if([strAddress isEqualToString:strTitle]) {
        [self presentModalViewController:self.nxtDetailsVCtr animated:YES];
        [self.nxtDetailsVCtr.lblDetail setText:strAddress];     
        break;
    }


}
    }
like image 565
Hiren gardhariya Avatar asked Jan 28 '26 12:01

Hiren gardhariya


1 Answers

You can use NVPlolineAnnotation to draw line inside the map. Here is the Source Code.

like image 70
Virus Avatar answered Jan 31 '26 02:01

Virus