【问题标题】:Multiple Markers Google Map iOS SDK多个标记 Google Map iOS SDK
【发布时间】:2014-10-06 08:05:31
【问题描述】:

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

【问题讨论】:

  • move camera = ..., mapView_ = ... 从循环中移出。每次创建新的 mapView

标签: ios objective-c google-maps


【解决方案1】:
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
     }
}

【讨论】:

    【解决方案2】:

    检查这个:

       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_];
    

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

    【讨论】:

    • 怎么样?因为我需要在循环中有 [loc.Latitude doubleValue] 和 [loc.Long doubleValue] 因为它包含我的位置的所有值?
    【解决方案3】:

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

    camera = [GMSCameraPosition cameraWithLatitude:[loc.Latitude doubleValue]
                                longitude:[loc.Long doubleValue]
                                     zoom:15];
    
    mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
    mapView_.myLocationEnabled = NO;
    

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

    【讨论】:

    • 怎么样?因为我需要在循环中有 [loc.Latitude doubleValue] 和 [loc.Long doubleValue] 因为它包含我的位置的所有值?
    【解决方案4】:

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

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多