2014-10-06 35 views
1

我很努力地在iOS 8.0的Google Map中获取多个标记。我当前的代码是:多个标记Google Map iOS SDK

ResourceGroep *rsg = [ResourceGroep alloc]init; 
GMSCameraPosition *camera = nil; 

for (int i = 0; i < [rsg.chosenResourceArray count]; i++) 
{ 
    camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue] 
           longitude:[loc.Long doubleValue] 
           zoom:15]; 

    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera]; 
    mapView_.myLocationEnabled = NO; 

    CLLocationCoordinate2D position = { [rsg.Latitude doubleValue], [rsg.Long doubleValue] }; 
    GMSMarker *marker = [GMSMarker markerWithPosition:position]; 
     marker.title = [NSString stringWithFormat:@"Marker %i", i]; 
     marker.appearAnimation = YES; 
     marker.flat = YES; 
     marker.snippet = @""; 
     marker.map = mapView_; 
} 

[self.view addSubview:mapView_]; 

我已经通过我的反复排列,但我只看到1个标记,而我的阵列数量就像是2或3取决于选择什么样的用户。我错过了什么?

+0

move camera = ...,mapView_ = ...从循环中移出。你每次创建新的mapView – kabarga 2014-10-06 08:12:45

回答

4

检查:

ResourceGroep *rsg = [ResourceGroep alloc]init; 
    GMSCameraPosition *camera = nil; 

    camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue] 
          longitude:[loc.Long doubleValue] 
           zoom:15]; 

    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera]; 
    mapView_.myLocationEnabled = NO; 

    for(int i = 0; i < [rsg.chosenResourceArray count]; i++) 
    { 




     CLLocationCoordinate2D position = { [rsg.Latitude doubleValue], [rsg.Long doubleValue] }; 
     GMSMarker *marker = [GMSMarker markerWithPosition:position]; 
     marker.title = [NSString stringWithFormat:@"Marker %i", i]; 
     marker.appearAnimation = YES; 
     marker.flat = YES; 
     marker.snippet = @""; 
     marker.map = mapView_; 
    } 

[self.view addSubview:mapView_]; 

我刚才已经改变了一些代码的位置。

+0

怎么样?因为我需要在循环内有[loc.Latitude doubleValue]和[loc.Long doubleValue],因为它包含我的位置的所有值? – 2014-10-07 07:56:47

0

将这些线放在循环之外应该有所帮助。

camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue] 
          longitude:[loc.Long doubleValue] 
           zoom:15]; 

mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera]; 
mapView_.myLocationEnabled = NO; 

您每次添加标记时都会初始化地图。初始化将清除迄今添加的所有标记。

+0

怎么样?因为我需要在循环内有[loc.Latitude doubleValue]和[loc.Long doubleValue],因为它包含我的位置的所有值? – 2014-10-07 07:30:17

0

我得到了最好的办法,希望它对你也有用。

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_sourceloc.latitude longitude:_sourceloc.longitude zoom:6]; 
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 


GMSMarker *marker = [GMSMarker markerWithPosition:_sourceloc]; 
[email protected]"Source"; 
marker.snippet =_sourceAdd; 
marker.appearAnimation = kGMSMarkerAnimationPop; 
marker.map = mapView; 


GMSMarker *marker2 = [GMSMarker markerWithPosition:_destinationloc]; 
[email protected]"Destination"; 
marker2.snippet =_destinationAdd; 
marker2.appearAnimation = kGMSMarkerAnimationPop; 
marker2.map = mapView; 

self.view = mapView; 
相关问题