且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在目标c中的地图上显示几个位置点

更新时间:2023-10-28 23:12:16

每个纬度,你都是经度绘制的。我删除了内部循环。你可以尝试下面的代码,看看是否适用于你?

  for(int x = 0; 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];


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];
    }
}    

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];