多个标记Google Map iOS SDK

时间:2014-10-06 08:05:31

标签: ios objective-c google-maps

我很难在iOS 8.0上的Google地图中获得多个标记。我目前的代码是:

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取决于用户选择的内容。我错过了什么?

4 个答案:

答案 0 :(得分: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_];

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

答案 1 :(得分:0)

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

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

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

每次添加标记时,都在初始化地图。初始化将清除目前为止添加的所有标记。

答案 2 :(得分:0)

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

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


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


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

self.view = mapView;

答案 3 :(得分:0)

func showmarkeronmap(){
    DispatchQueue.main.async {
        let first = 0
        let last = self.vehicallistArr.count
        let interval = 1
        let sequence = stride(from: first, to: last, by: interval)
        for element in sequence {
            let cateAryrray = self.vehicallistArr[element]
            let mylatitude = Double(cateAryrray.carLatitude!)
            let mylongitude = Double(cateAryrray.carLongitute!)
            let camera = GMSCameraPosition.camera(withLatitude: mylatitude!, longitude: mylongitude!, zoom: 10.0)
             self.googleMapView.camera = camera
            showMarker(position: camera.target, markerTitle: cateAryrray.dailypriceStr!, markerSnippet: cateAryrray.titleStr!)
     }
        let update = GMSCameraUpdate.fit(self.bounds, withPadding: 30.0)
        self.googleMapView.animate(with: update)
}


func showMarker(position: CLLocationCoordinate2D, markerTitle : String , markerSnippet : String){
        let marker = GMSMarker()
        marker.position = position
        marker.title = markerTitle
        marker.snippet = markerSnippet
        marker.map = self.googleMapView
        self.googleMapView.selectedMarker = marker
     }
}