我希望无论地图的位置坐标如何,都能将标记固定在地图中心。如果用户在地图上移动相机,我希望它仍然显示在中心位置,而不会闪烁,并且该标记上的新位置也会显示出来。我该怎么做?请帮忙。
我希望无论地图的位置坐标如何,都能将标记固定在地图中心。如果用户在地图上移动相机,我希望它仍然显示在中心位置,而不会闪烁,并且该标记上的新位置也会显示出来。我该怎么做?请帮忙。
mapView.delegate=self;
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;
}
如何使标记不移动
基于GMSMapView的框架,在中心位置创建一个ImageView或可点击的Button。
如果您想获取坐标,可以使用 mapView.projection.coordinateForPoint
。
这将移动标记。
通过以下代码查找Google地图的中心点:
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
}
尝试以下代码
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 ;)
}