How to rotate custom marker image on google map ob

2020-06-22 08:07发布

Currently i am working an app like Uber iOS application. I Already integrated Google Maps SDK and I showed custom image for User current location also. Currently I am getting some Driver's Current location details(Ex: 100 Driver's) from server. I saved in one NSArray and I tried to display those Lat & Long on GoogleMaps by using following code:

for(int i=0;i<[latLongArr count];i++)
{
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake([[(NSDictionary *)[latLongArr objectAtIndex:i] valueForKey:@"Latitude"] doubleValue], [[(NSDictionary *)[latLongArr objectAtIndex:i] valueForKey:@"Longitude"] doubleValue]);
    marker.appearAnimation = kGMSMarkerAnimationPop;
    marker.title = @"Title";
    marker.snippet = @"Sub title";
    marker.map = self.gMapView;
}

But I am looking UIDesign & Functionality like this:

Can Any one help me out how can I show User Current location & Driver's list of Annotations
(How to rotate custom marker image on google map)

1条回答
老娘就宠你
2楼-- · 2020-06-22 08:34

In directions API (apple or google map) have a list points. So , to calculate the angle between two points, you can:

func DegreeBearing(A:CLLocation,B:CLLocation)-> Double{
    var dlon = self.ToRad(B.coordinate.longitude - A.coordinate.longitude)
    let dPhi = log(tan(self.ToRad(B.coordinate.latitude) / 2 + M_PI / 4) / tan(self.ToRad(A.coordinate.latitude) / 2 + M_PI / 4))
    if  abs(dlon) > M_PI{
        dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon)
    }
    return self.ToBearing(atan2(dlon, dPhi))
}

func ToRad(degrees:Double) -> Double{
    return degrees*(M_PI/180)
}

func ToBearing(radians:Double)-> Double{
    return (ToDegrees(radians) + 360) % 360
}

func ToDegrees(radians:Double)->Double{
    return radians * 180 / M_PI
}

and set rotation for maker

maker.rotation = DegreeBearing(self.fromPoint, B: self.toPoint)

Updated ObjC Code below

-(double) DegreeBearing:(CLLocation*) A locationB: (CLLocation*)B{
    double dlon = [self ToRad:(B.coordinate.longitude - A.coordinate.longitude)];
    double dPhi = log(tan([self ToRad:(B.coordinate.latitude)] / 2 + M_PI / 4) / tan([self ToRad:(A.coordinate.latitude)] / 2 + M_PI / 4));
    if  (fabs(dlon) > M_PI){
        dlon = (dlon > 0) ? (dlon - 2*M_PI) : (2*M_PI + dlon);
    }
    return [self ToBearing:(atan2(dlon, dPhi))];
}

-(double) ToRad: (double)degrees{
    return degrees*(M_PI/180);
}

-(double) ToBearing:(double)radians{
    double degree = [self ToDegrees:radians];
    return degree+360% 360;
}

-(double) ToDegrees:(double)radians{
    return radians * 180 / M_PI;
}
查看更多
登录 后发表回答