【问题标题】:Multiple marker is not showing多个标记未显示
【发布时间】:2018-10-05 12:21:31
【问题描述】:

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

注意:我做了一些改动,代码运行完美。

我的代码是:

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

}

【问题讨论】:

  • 你在哪里声明driveMarker?似乎您只有其中一个,因此在您的 for 循环中,您总是会覆盖该值,而旧的值会被 ARC 释放。
  • driverMarker 在循环中..我应该在视图加载时使用它吗?..我在 viewdidload 处分配初始化 driverMarker 但没有任何变化..标记没有显示

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


【解决方案1】:

我猜你的 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 属性来存储所有创建的标记,只是为了将它们保持在范围内。

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2016-09-15
    • 2017-11-04
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多