【发布时间】:2017-08-30 13:37:50
【问题描述】:
无论位置坐标如何,我都想将标记固定在地图的中心。如果用户在地图上移动相机,我希望它继续显示在中心而没有任何闪烁的标记和该标记显示的新位置,我该怎么做?请帮忙。
【问题讨论】:
标签: ios objective-c iphone google-maps-markers google-maps-sdk-ios
无论位置坐标如何,我都想将标记固定在地图的中心。如果用户在地图上移动相机,我希望它继续显示在中心而没有任何闪烁的标记和该标记显示的新位置,我该怎么做?请帮忙。
【问题讨论】:
标签: ios objective-c iphone google-maps-markers google-maps-sdk-ios
试试下面的 Objective C 代码
别忘了设置委托
mapView.delegate=self;
创建 ImageView 并将其添加到地图的中心
UIImageView *pin =[[UIImageView alloc]init];
pin.frame=CGRectMake(0, 0, 20, 20 );
pin.center = mapView.center;
pin.image = [UIImage imageNamed:@"location.png"];
[self.view addSubview:pin];
[self.view bringSubviewToFront:pin];
然后使用谷歌地图的委托方法
//When You Will Scroll Map Then This Method Will Be Called
- (void)mapView:(GMSMapView *)MapView didChangeCameraPosition:(GMSCameraPosition *)position {
// Get Latitude And Longitude Of Your Pin(ImageView)
CLLocationCoordinate2D newCoords = CLLocationCoordinate2DMake( position.target.latitude , position.target.longitude);
NSLog(@"Latitude-%f\Longitude-%f\n",newCoords.latitude, newCoords.longitude);
[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(newCoords.latitude, newCoords.longitude) completionHandler:^(GMSReverseGeocodeResponse* response, NSError* error) {
//Get Place Details
NSLog(@"%@",[[response results]objectAtIndex:0].lines);
}];
return;
}
【讨论】:
试试下面的代码
GMSCameraPosition *cameraPosition;
- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
/* move draggable pin */
if (fixedMarker) {
// stick it on map and start dragging from there..
if (lastCameraPosition == nil) lastCameraPosition = position;
// Algebra :) substract coordinates with the difference of camera changes
double lat = position.target.latitude - lastCameraPosition.target.latitude;
double lng = position.target.longitude - lastCameraPosition.target.longitude;
lastCameraPosition = position;
CLLocationCoordinate2D newCoords = CLLocationCoordinate2DMake(fixedMarker.googleMarker.position.latitude+lat,
fixedMarker.googleMarker.position.longitude+lng);
[fixedMarker.googleMarker setPosition:newCoords];
return;
}
}
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
cameraPosition = nil; // reset pin moving, no ice skating pins ;)
}
【讨论】:
不移动标记
根据 GMSMapView 的框架在 GMSMapView 的中心创建一个 Imageview 或 Button(如果可点击)。
如果你想获取坐标,你可以使用mapView.projection.coordinateForPoint
这将移动标记
通过以下代码找到谷歌地图的中心点
let centerCord = yourMapView.camera.target
let marker = GMSMarker(position: centerCord)
marker.title = "Hello World"
marker.map = mapView
实现mapView:didChangeCameraPosition:
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
// to remove all markers
mapView.clear()
//create new marker with new center
let centerCord = [yourMapView.camera target]
let marker = GMSMarker(position: centerCord)
marker.title = "Hello World"
marker.map = mapView
}
【讨论】: