Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapview live car tracking

My requirement is to create an app that show live tracking of cab. Like famous car apps like Ola , uber and so on.

Please let me know how to update annotation , even for street turn and car reverse . How can simulate moving annotation using MKMapview. any library i have to use. I searched but i couldn't find any library

like image 276
Vinodh Avatar asked Nov 02 '16 15:11

Vinodh


Video Answer


2 Answers

As I think the problem is smooth turning of the annotation on the map. As you can place your own custom image instead of the default blue dot.

for smooth turning you can use CMMotionManager as it gives you the acceleration so you can rotate the image by taking the reference of the annotation view. You can update for acceleration data by using update devicemotion data. As you get the useracceleration along x, y and z you can obtain the angle by tan function.This should solve your problem

code for getting the angle

[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    double angle = atan(motion.userAcceleration.x/motion.userAcceleration.y);
}];
like image 84
santhosh Avatar answered Oct 04 '22 15:10

santhosh


The solution to this problem is quite trivial. If you having problem implementing this library, why not to do it yourself and learn something? You just create some type of Vehicle model class that stores coordinate and previous coordinate. To be able to display it on the map it would have to adhere to MKAnnotation protocol - implement: title, subtitle and coordinate. The newCoordinate position will be set by default upon obtaining position data from network. You need to track two values to successfully animate.

and thus implement something like this:

@interface Vehicle : NSObject <MKAnnotation>

@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, assign) CLLocationCoordinate2D newCoordinate;

@end

Upon setting the newCoordinate you move the previous value from custom setter into the coordinate property. Once you do this, you would just animate the annotation as usual.

// new coordinate obtained from networking
- (void)setNewCoordinate:(CLLocationCoordinate2D)newCoordinate {

    // implement the value moving part

    _newCoordinate = newCoordinate;
}

But be careful when detecting taps on the animated annotation, because of the way it works. Annotation frame will be set when animation starts to the value of finished frame. You would need to hitTest taps on the presentationLayer of the annotation which gets displayed on the Screen during the animation.

To handle the taps override

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
}

animate with

[UIView animateWithDuration:0 
                      delay:0 
                    options:UIViewAnimationOptionCurveEaseIn 
                 animations:^{

} completion:nil];

I am sorry but I can't post code here since I have previously implemented this for my employer and am bound by contract.

like image 32
ha100 Avatar answered Oct 04 '22 15:10

ha100