本文介绍了iOS,如何使用GMSCoordinateBounds显示地图的所有标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想显示地图上的所有标记,进行一些搜索之后,我发现它应该使用 GMSCoordinateBounds (Google Maps SDK)完成。我已阅读有关它的官方文档,但我不明白如何使用它并在我的代码中实现它。 https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update#aa5b05606808272f9054c54af6830df3e 这是我的代码 GMSCoordinateBounds * bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D位置; for(NSDictionary * dictionary in array){ location.latitude = [dictionary [@latitude] floatValue]; location.longitude = [字典[@longitude] floatValue]; //在地图的中心创建一个标记。 GMSMarker * marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@marker.png)]; marker.position = CLLocationCoordinate2DMake(location.latitude,location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary [@type]; marker.map = mapView_; } [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; 任何帮助? 解决方案 - (void)focusMapToShowAllMarkers { GMSCoordinateBounds * bounds = [[GMSCoordinateBounds alloc]在里面]; for(GMSMarker * marker in<您的标记数组>) bounds = [bounds includingCoordinate:marker.position]; [< yourMap> animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; $ b} 更新: 你确定你的标记数组和坐标没有错吗? 我试过这段代码,并且工作正常。我已经把它放在viewDidAppear上了。 $ b $ pre $ NSMutableArray * array = [[NSMutableArray alloc] initWithObjects:[[NSDictionary alloc] initWithObjectsAndKeys: @44.66,@latitude,@21.33,@longitude,nil], [[NSDictionary alloc] initWithObjectsAndKeys:@44.66,@latitude,@21.453 经度,nil], [[NSDictionary alloc] initWithObjectsAndKeys:@44.44,@latitude,@21.993,@longitude,nil], [[NSDictionary alloc] initWithObjectsAndKeys:@44.635,@latitude,@21.553,@longitude,nil], [[NSDictionary alloc] initWithObjectsAndKeys:@44.3546,@latitude ,@longitude,nil], [[NSDictionary alloc] initWithObjectsAndKeys:@44.6643,@latitude,@21.212,@longitude,nil] alloc] initWithObjectsAndKeys:@44.63466,@latitude,@21.3523,@longitude,nil],nil]; GMSCoordinateBounds * bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D位置; for(NSDictionary * dictionary in array) { location.latitude = [dictionary [@latitude] floatValue]; location.longitude = [字典[@longitude] floatValue]; //在地图的中心创建一个标记。 GMSMarker * marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@marker.png)]; marker.position = CLLocationCoordinate2DMake(location.latitude,location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary [@type]; marker.map = mapView_; } [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]]; 这是我的结果: I want to show all the markers that are on my map, after doing some searches I found that it should be done with GMSCoordinateBounds (Google Maps SDK) I've read the official documentation about it, but I have not understand how to use it and implement it in my code.https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update#aa5b05606808272f9054c54af6830df3eHere is my codeGMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; CLLocationCoordinate2D location;for (NSDictionary *dictionary in array) { location.latitude = [dictionary[@"latitude"] floatValue]; location.longitude = [dictionary[@"longitude"] floatValue]; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@"marker.png")]; marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary[@"type"]; marker.map = mapView_;} [mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];Any help ? 解决方案 - (void)focusMapToShowAllMarkers{ GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init]; for (GMSMarker *marker in <An array of your markers>) bounds = [bounds includingCoordinate:marker.position]; [<yourMap> animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];}UPDATE:are you sure there is nothing wrong in you array of markers and the coordinates?I've tried this code and is working perfectly. I've put this on the viewDidAppearNSMutableArray *array = [[NSMutableArray alloc]initWithObjects:[[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.33",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.66",@"latitude",@"21.453",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.44",@"latitude",@"21.993",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.635",@"latitude",@"21.553",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.3546",@"latitude",@"21.663",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.6643",@"latitude",@"21.212",@"longitude", nil], [[NSDictionary alloc]initWithObjectsAndKeys:@"44.63466",@"latitude",@"21.3523",@"longitude", nil],nil];GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];CLLocationCoordinate2D location;for (NSDictionary *dictionary in array){ location.latitude = [dictionary[@"latitude"] floatValue]; location.longitude = [dictionary[@"longitude"] floatValue]; // Creates a marker in the center of the map. GMSMarker *marker = [[GMSMarker alloc] init]; marker.icon = [UIImage imageNamed:(@"marker.png")]; marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); bounds = [bounds includingCoordinate:marker.position]; marker.title = dictionary[@"type"]; marker.map = mapView_;}[mapView_ animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];This is my result: 这篇关于iOS,如何使用GMSCoordinateBounds显示地图的所有标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 23:57