OStack程序员社区-中国程序员成长平台

标题: ios - 如何同步移动标记和相机并始终将标记放置在相机位置IOS的中心 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:49
标题: ios - 如何同步移动标记和相机并始终将标记放置在相机位置IOS的中心

我希望 GMSMarker 移动到相机。这是我的代码

 -(void)addMap {

        GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
                                                                longitude:longitude
                                                                     zoom:15];
        GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
        CGRect newFrame = CGRectMake( 0, 0, [Util window_width],[Util window_height]);
        mapView = [GMSMapView mapWithFrame:newFrame camera:camera];
        mapView.delegate = self;
        mapView.myLocationEnabled = YES;

        GMSMarker *marker = [[GMSMarker alloc] init];
        marker.position = camera.target;
        marker.snippet = @"Hello World";
        marker.icon =[Util imageWithImage:[UIImage imageNamed"set_address.png"] scaledToSize:CGSizeMake(170, 65)];
        marker.appearAnimation = kGMSMarkerAnimationPop;
        marker.map = mapView;
        [self.MapContentView addSubview:mapView];


     }

我在 map 上有一个标记,如果用户不滚动 map ,它通常位于 map 的中心。

当用户滚动 map 时,我希望标记与相机一起移动到新位置,因此它始终位于 map 的中心。

我试试下面的代码

-(BOOL) mapViewGMSMapView *) mapView didTapMarkerGMSMarker *)marker
{
    [mapView animateToLocation:marker.position];


    return YES;
}


- (void)mapViewGMSMapView *)mapView didChangeCameraPositionGMSCameraPosition *)position
{

    marker.position = camera.target;
    marker.appearAnimation = kGMSMarkerAnimationPop;

    NSLog(@"camera.target.latitude %f,%f",camera.target.latitude,camera.target.longitude);

    latitude=camera.target.latitude;
    longitude=camera.target.longitude;

}



Best Answer-推荐答案


你可以试试这个:

- (void)mapViewGMSMapView *)mapView willMoveBOOL)gesture
{
    [self recenterMarkerInMapView:mapView];
}

- (void)mapViewGMSMapView *)mapView didChangeCameraPositionGMSCameraPosition *)position
{
    [self recenterMarkerInMapView:mapView];
}

- (void)recenterMarkerInMapViewGMSMapView *)mapView
{
    // Get the center of the mapView
    CGPoint center = [mapView convertPoint:mapView.center fromView:self.view];

    // Reset the marker position so it moves without animation
    [mapView clear];
    marker.appearAnimation = kGMSMarkerAnimationNone;
    marker.position = [mapView.projection coordinateForPoint:center];
    marker.map = mapView;
}

关于ios - 如何同步移动标记和相机并始终将标记放置在相机位置IOS的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375672/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4