man*_*mar 8 google-maps objective-c uiimage google-maps-markers ios
我已在我的应用程序中集成了Google地图,并使用了Google Places API.在我从Google Places API(大约60)获得所有结果后,我将在自定义标记的帮助下显示它们.我制作的自定义标记包含"Place Image"和"Place Name",因为我必须首先在UIView中绘制它,然后借助以下函数将其渲染为UIImage
- (UIImage *)imageFromView:(UIView *) view
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
} else {
UIGraphicsBeginImageContext(view.frame.size);
}
[view.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Run Code Online (Sandbox Code Playgroud)
首先,所有标记都可以轻松渲染和绘制.
现在我有一个100米到5千米的滑块,它可以作为搜索半径优化器.当滑块移动时(假设值为2km),则移除所有标记,并且仅再次绘制与用户位置的距离小于滑块值的那些标记.当我测试滑块功能时,应用程序崩溃说
((null))为false:达到纹理图集的最大数量,不能分配更多.
我正在上传屏幕截图,以便清楚了解情况.请帮忙.
另外,在屏幕中,您将看到绿色标记以及蓝色标记.蓝色标记是靠近用户位置的标记,而绿色标记远离特定距离.由于用户位置将发生变化,因此有两种情况:
我正在开发一个应用程序,它可以有数千个头像在地图上移动,并且也会遇到这个错误。集群是一个潜在的解决方案,但由于这么多化身都在移动,我怀疑计算会过于占用 CPU 资源。
我使用的解决方案是保留对基本头像图像的引用,并在屏幕上出现超过 50 个头像时使用它。仅当屏幕上的头像少于 50 个时,我才会为每个头像生成唯一的图像及其名称。
// GMSMarker
static var avatarDic:[String:UIImage] = Dictionary()
func removeName() {
// use a single image reference here so that google map does not crash
if let image = avatarDic[avatarBase] {
self.icon = image
}
else {
avatarDic[avatarBase] = UIImage(named:avatarBase)
self.icon = avatarDic[avatarBase]
}
}
func addName() {
self.icon = // draw name on base image
}
// GMSMapView
var userIcons:[String:MyMarker] = Dictionary()
var iconWithNames:Set<MyMarker> = Set()
func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
// find visible avatars til limit
let bottomLeft = self.mapView.projection.visibleRegion().nearLeft
let topRight = self.mapView.projection.visibleRegion().farRight
var visibleMarkerSet:Set<MyMarker> = Set()
for (key, marker) in self.userIcons {
if (marker.position.latitude > bottomLeft.latitude && marker.position.latitude < topRight.latitude && marker.position.longitude > bottomLeft.longitude && marker.position.longitude < topRight.longitude) {
visibleMarkerSet.insert(marker)
}
// not showing if > 50
if (visibleMarkerSet.count > 50) {
visibleMarkerSet = Set()
break
}
}
// remove names
for markerWithName in self.iconWithNames {
if (visibleMarkerSet.contains(markerWithName) == false) {
markerWithName.removeName()
}
}
// add names
for visibleMarker in visibleMarkerSet {
visibleMarker.addName()
}
self.iconWithNames = visibleMarkerSet
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1806 次 |
| 最近记录: |