5

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)

nur farazi
  • 1,197
  • 12
  • 32
Mannam Brahmam
  • 2,225
  • 2
  • 24
  • 36
  • You mush have a server to update location of car Or You can get current location & target location, speed from server , after that get direction and motion simulator with the speed – Nguyen Hoan Dec 29 '15 at 10:43
  • Yes. I already update user & driver current location to Server.. My question is (How to rotate custom marker image on google map) . If User open the app those all Car images will be rotate 360 degrees on Google Maps ---> That's what i am looking – Mannam Brahmam Dec 29 '15 at 10:45
  • Create NSthreads for each driver and user as well. Then update their location depending upon the data coming from server with some condition check for instance, if location of driver is within the radius of user then show else skip. Best is to first write down some algorithm or seudo code for this before directly coding. – nikhil84 Dec 29 '15 at 10:47
  • In server side i am getting only Lat & Long for Driver's --> My requirement is i want to show those list on Google Maps (Already i did that). But the one more future like Direction API---> Car is moving Horizontal but (Car symbol is showing some times Vertical/Reverse and etc) but it's not rotate custom marker image as per car direction – Mannam Brahmam Dec 29 '15 at 10:51
  • @MannamBrahmam.How u moving car direction(rotation) as user moves. – Uma Madhavi Dec 08 '16 at 05:42
  • @Uma Madhavi, Please refer this link: http://stackoverflow.com/a/35618161/4720315 – Mannam Brahmam Dec 08 '16 at 05:56
  • @MannamBrahmam.. Thank you for your response. I am working with swift.I am unable to move or rotate marker, everything works fine. – Uma Madhavi Dec 08 '16 at 05:59
  • @Uma Madhavi, I used same code in Objective-C, it's working fine for me. But I don't have good knowledge on Swift. Try to convert, If not I will try to a sample demo. – Mannam Brahmam Dec 08 '16 at 06:01
  • @MannamBrahmam. Thank you.. – Uma Madhavi Dec 08 '16 at 06:03

1 Answers1

7

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(degrees: B.coordinate.longitude - A.coordinate.longitude)
    let dPhi = log(tan(self.ToRad(degrees: B.coordinate.latitude) / 2 + M_PI / 4) / tan(self.ToRad(degrees: 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(radians: atan2(dlon, dPhi))
}

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

func ToBearing(radians:Double)-> Double{
    return (ToDegrees(radians: radians) + 360).truncatingRemainder(dividingBy: 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;
}
Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
Nguyen Hoan
  • 1,583
  • 1
  • 11
  • 18