2014-10-06 72 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; 
相關問題