How can I show several location points on map in objective c

三世轮回 提交于 2019-12-13 02:59:29

问题


I have two NSMutableArray, first keeps some location's latitudes and second keeps longitudes. Then, I want to see this locations on map with markers. I try like this but it is not work. How can I do this?

    for (int x=0; x<=[ws8.Latitude count]; x++) {
    for (int y=0; y<=[ws8.Longitude count]; y++) {

        CLLocationCoordinate2D location1;

        location1.latitude = [[ws8.Latitude objectAtIndex:x] floatValue];
        location1.longitude = [[ws8.Longitude objectAtIndex:y] floatValue];


        region.span=span;
        region.center=location1;

        if(addAnnotation != nil) {
            [mapView removeAnnotation:addAnnotation];
            [addAnnotation release];
            addAnnotation = nil;
        }
        addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location1];
        [mapView addAnnotation:addAnnotation];
        [mapView setRegion:region animated:TRUE];
        [mapView regionThatFits:region];
        [mapView setShowsUserLocation:YES];
    }
}    

回答1:


for each latitude, you are mapping with ever longitude. I removed the inner loop. Can you try below code and see if that works for you?

for (int x=0; x<=[ws8.Latitude count]; x++) {
    CLLocationCoordinate2D location1;

    location1.latitude = [[ws8.Latitude objectAtIndex:x] floatValue];
    location1.longitude = [[ws8.Longitude objectAtIndex:x] floatValue];


    region.span=span;
    region.center=location1;

//        if(addAnnotation != nil) {
//            [mapView removeAnnotation:addAnnotation];
//            [addAnnotation release];
//            addAnnotation = nil;
//        }
    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location1];
    [mapView addAnnotation:addAnnotation];
    [addAnnotation release];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
}


[mapView setShowsUserLocation:YES];


来源:https://stackoverflow.com/questions/7790208/how-can-i-show-several-location-points-on-map-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!