问题描述
我正在使用 GMSMapView .因此,我添加了自定义 GMSMarker 并设置了图像(例如Bike图像),并在用户开始在locationManager中移动和更改标记的角度时对该标记进行动画处理.
I'm using GMSMapView. So I added custom GMSMarker and set the image(Ex. Bike image) and animating that marker when user starts moving and changing the angle of the marker in locationManager .
当前,我正在更新GMSMarker的position属性. 但是,它具有GMSMarker跳跃效果. 代替此操作,我想平滑/正确地移动并旋转 GMSMarker到GMSMapView.
Currently I'm just updating position attribute of GMSMarker. But it gives the GMSMarker jump effect. Instead of this I want to move and rotate the GMSMarker smoothly/properly into the GMSMapView.
我该如何在Swift中实现这一目标?谢谢.
How can i achieve this in Swift? Thanks.
(void)updateLocationoordinates(CLLocationCoordinate2D) coordinates
{
if (marker == nil) {
marker = [GMSMarker markerWithPosition:coordinates];
marker.icon = [UIImage imageNamed:IMAGE];
marker.map = mapView;
} else{
marker.position = coordinates;
}
}
推荐答案
我遇到了同样的问题,并尝试了许多方法.最后,我想出了一个解决方案-在先前的位置点和当前的位置点之间取一个角度,并且我使用了UIImageView而不是GMSMarker.
I've faced the same problem and tried many ways. At last I've came up with a solution - to take angle between previous location point and present location point and I've used an UIImageView instead of GMSMarker.
//in location update method
CGPoint anglepoint = [myMapView.projection pointForCoordinate:currLocation.coordinate];
CGFloat angle = [self getAngle:anglepoint];
if(!isnan(angle)){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8f];
sampleImgView.transform = CGAffineTransformMakeRotation(angle);
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
[UIView commitAnimations];
prevCurrLocation = currLocation;
}
-(CGFloat) getAngle: (CGPoint) touchedPoints
{
CGPoint previousLocationPoint = [myMapView.projection pointForCoordinate:prevCurrLocation.coordinate];
CGFloat x1 = previousLocationPoint.x;
CGFloat y1 = previousLocationPoint.y;
CGFloat x2 = touchedPoints.x;
CGFloat y2 = touchedPoints.y;
CGFloat x3 = x1;
CGFloat y3 = y2;
CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));
CGFloat angle = atanf(oppSide/adjSide);
// Quadrant Identifiaction
if(x2 < previousLocationPoint.x)
{
angle = 0-angle;
}
if(y2 > previousLocationPoint.y)
{
angle = M_PI/2 + (M_PI/2 -angle);
}
return angle;
}
使用此动画:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
sampleImgView.transform = CGAffineTransformMakeRotation(angle);
[sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
[UIView commitAnimations];
这是示例Imageview:
Here is the sample Imageview:
sampleImgView = [[UIImageView alloc]init];
sampleImgView.center = myMapView.center;
sampleImgView.frame = CGRectMake(0,0, 25, 50);
sampleImgView.backgroundColor = [UIColor clearColor];
[myMapView addSubview:sampleImgView];
希望这会有所帮助.
这篇关于沿最后更新的GPS坐标平滑地移动和旋转GMSMarker Swift iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!