多个标记未显示

时间:2018-04-25 06:17:23

标签: ios objective-c google-maps-markers google-maps-sdk-ios

我想在谷歌地图上显示多个标记。基于此有答案。但标记没有显示在地图上。虽然我根据数组结果得到纬度和经度值。我该怎么办?

注意:我做了一些更改,代码运行得很好。

我的代码是:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self performRequestForRestaurantListing];
    geometryDict=[[NSMutableDictionary alloc]init];
    locationDict=[[NSMutableDictionary alloc]init];

    NSLog(@"the value of list is %@", _service);
    NSLog(@"the value of stringradius is %@", _stringRadius);

    /*---location Manager Initialize-------*/
    self.manager=[[CLLocationManager alloc]init];
    self.manager.distanceFilter = 100;
    self.manager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.manager requestAlwaysAuthorization];
    self.manager.delegate=self;
    [self.manager startUpdatingLocation];


    [mapView setDelegate:self];

    latitude=@"22.5726";
    longitude=@"88.3639";



    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude  doubleValue]
                                                           longitude:[longitude  doubleValue]
                                                                zoom:12];

    [mapView animateToCameraPosition:camera];
   [self coordinateOnMap:latitude andWithLongitude:longitude];
}

-(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude
{
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
    CLLocationCoordinate2D location;
   for (int i=0;i<[restaurantList count];i++)
    {
         driverMarker = [[GMSMarker alloc] init];
        latitude=[[[[restaurantList objectAtIndex:i]objectForKey:@"geometry"]objectForKey:@"location"] objectForKey:@"lat"];
        longitude=[[[[restaurantList objectAtIndex:i]objectForKey:@"geometry"]objectForKey:@"location"] objectForKey:@"lng"];
        location.latitude = [latitude floatValue];
        location.longitude = [longitude floatValue];
        driverMarker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
        driverMarker.map = mapView;    
    }
   driverMarker.icon=[UIImage imageNamed:@"marker"];
    bounds = [bounds includingCoordinate:driverMarker.position];
    driverMarker.title = @"My locations";
    [driverMarker setTappable:NO];
    mapView.myLocationEnabled = YES;

}

1 个答案:

答案 0 :(得分:0)

我猜你的driveMarker会在每次循环后立即被ARC取消分配。 如果这确实是你的问题,你必须确保这些标记“在循环中存活”,例如使用以下代码:

@implementation MyController
    @property (nonatomic) NSMutableArray *allMarkers;

    - (void)viewDidLoad {
         allMarkers = [[NSMutableArray alloc] init];
         // ...
    }

    -(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude {
        //...
        [allMarkers removeAllObjects];
        for (int i=0;i<[restaurantList count];i++) {
            GMSMarker *driverMarker =  [[GMSMarker alloc] init];
            [allMarkers addObject:driveMarker];

            // ...
         }
    }
@end

这将创建一个NSArray属性来存储所有已创建的标记,只是为了将它们保留在范围内。