Objectivec固定标记谷歌地图中心ios
问题描述:
我想修复标记在地图的中心,无论位置坐标。如果用户在地图上移动相机,我希望它在中间不会出现任何闪烁的标记,并且该标记上的新位置会显示,我该怎么做?请帮忙。Objectivec固定标记谷歌地图中心ios
答
试试下面的代码
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 ;)
}
+1
嗨Basir,它工作正常,但如果我移动地图,标记也移动并固定在中心。但我希望它能继续在中心显示出来,而不会有任何闪烁的痕迹。 – Karthick
答
为了不移动标记
创建的ImageView或按钮(如果可点击)的基础上GMSMapView中的框架上的GMSMapView中的中心。
,如果你想要得到的坐标,您可以使用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
}
答
尝试以下目标C代码
不要忘记设置Delagate
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;
}
看到这个https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial –