iOS Route-Me: Add User Location Marker - ios

I have been trying to add the following things to a Route-Me app.
Add a marker at the starting location
Move the map to the user location
Move the marker to that new location
I am using the basic example from MapBox ios example, as my maps are from an offline mbtiles store.
This is my header file:
#import <UIKit/UIKit.h>
#import "RMMapView.h"
#import "RMMarker.h"
#import "RMMapViewDelegate.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#interface MBTiles_ExampleViewController : UIViewController
{
RMMapView *mapView;
}
#property (nonatomic, retain) IBOutlet RMMapView *mapView;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) CLLocation *currentLocation;
#property (nonatomic, retain) RMMarkerManager *markerManager;
#property (nonatomic, retain) RMMarker *locationMarker;
#end
And this is my implementation file:
#define kStartingLat 30.0f
#define kStartingLon -10.0f
#define kStartingZoom 1.5f
#import "MBTiles_ExampleViewController.h"
#import "RMMBTilesTileSource.h"
#import "RMMapContents.h"
#import "RMMarker.h"
#import "RMMarkerManager.h"
#import "CoreLocation/CoreLocation.h"
#implementation MBTiles_ExampleViewController
#synthesize mapView;
#synthesize currentLocation;
#synthesize locationManager;
#synthesize markerManager;
#synthesize locationMarker;
(void)viewDidLoad
{
CLLocationCoordinate2D startingPoint;
startingPoint.latitude = kStartingLat;
startingPoint.longitude = kStartingLon;
NSURL *tilesURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"control-room-0.2.0" ofType:#"mbtiles"]];
RMMBTilesTileSource *source = [[[RMMBTilesTileSource alloc] initWithTileSetURL:tilesURL] autorelease];
[[[RMMapContents alloc] initWithView:self.mapView
tilesource:source
centerLatLon:startingPoint
zoomLevel:kStartingZoom
maxZoomLevel:[source maxZoom]
minZoomLevel:[source minZoom]
backgroundImage:nil] autorelease];
mapView.enableRotate = NO;
mapView.deceleration = NO;
mapView.backgroundColor = [UIColor blackColor];
mapView.contents.zoom = kStartingZoom;
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
UIImage *iconImage = [UIImage imageNamed:#"marker.png"];
locationMarker = [[RMMarker alloc] initWithUIImage: iconImage];
[markerManager addMarker: locationMarker AtLatLong: startingPoint];
}
(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[mapView moveToLatLong:newLocation.coordinate];
RMLatLong newCoords = {newLocation.coordinate.latitude, newLocation.coordinate.longitude};
if (nil != markerManager)
[markerManager moveMarker:locationMarker AtLatLon: newCoords];
}
(void)dealloc
{
[mapView release];
[super dealloc];
}
#end
The marker.png has been added to my resources folder.
So my questions
Why is my starting marker not showing?
I am using xcode on SnowLeopard, so can the simulator actually find my location? As the map does not move.
Any help would be great as I have tried so many code snippets and tutorials but none have ended up working.

Regarding your second question, from the apple docs:
In Xcode 4.0 and 4.1, you could simulate only the current location in your application. As of Xcode 4.2, you can simulate locations other than your current location in iOS applications that use Core Location. To set a location, choose Edit Scheme from the scheme selector in the toolbar, select the Run action, and click the Options tab. You can then choose a location from the Location menu
http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_2.html

I was able to get it working after cleaning marker after move.
marker = [[RMMarker alloc] initWithUIImage:[UIImage imageNamed:#"marker.png"] anchorPoint:CGPointMake(0.5f, 1.f)];
[mapView.contents.markerManager addMarker:marker AtLatLong:locPoland];
[mapView.contents.markerManager moveMarker:marker AtLatLon:locWawa];
[mapView.markerManager moveMarker:marker AtLatLon: locUser];
[mapView moveToLatLong:locUser];
marker = nil;

Related

Measuring the distance between latest coordinates with a fixed one in iOS showing larger distance while they are very near

I am getting inaccurate result while measuring distance between latest coordinate and some a fixed coordinate which can be done by taping on the "Set fixed location" button.
Initially the distance is 0.0 meter as no location is fixed which is the first screenshoot below. Once I tap on the "Set fix location" button, it sets the the coordinate where I am standing at the time of pressing the button and the distance is immediately started calculated which is in the second screenshoot.
When I walk around 10 meters or more and come back to the position where I took the initial fixed location coordinate, its showing me more than 7 meters distance, although it should be less than half meter. I am testing this demo app outside in an open ground.
I have pasted all my code below and could you please help me to fixed the issue? I am new in GPS location related world and please kindly let me know if I am missing anything in the code. Are there any other ways to achieve more accurate result? I have been checking many stack-overflow articles and none help me to improve in my case.
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) CLLocation *fixedLocation;
#property (strong, nonatomic) CLLocation *latestLocation;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UILabel *fixedLocationLatitude;
#property (weak, nonatomic) IBOutlet UILabel *fixedLocationLongitude;
#property (weak, nonatomic) IBOutlet UILabel *latestLocationLatitude;
#property (weak, nonatomic) IBOutlet UILabel *latestLocationLongitude;
#property (weak, nonatomic) IBOutlet UILabel *distanceBetweenLocations;
- (IBAction)setFixLocation:(UIButton *)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.fixedLocation = [[CLLocation alloc] initWithLatitude:90.0 longitude:0.0]; // North pole if no location is fixed for the first time
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
if([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
if(locations.lastObject!=nil){
self.latestLocation = locations.lastObject;
self.latestLocationLatitude.text = [NSString stringWithFormat:#"%.8f", self.latestLocation.coordinate.latitude];
self.latestLocationLongitude.text = [NSString stringWithFormat:#"%.8f", self.latestLocation.coordinate.longitude];
}
if(self.fixedLocation != nil && self.latestLocation != nil){
CLLocationDistance distanceInMeter = [self.latestLocation distanceFromLocation:self.fixedLocation];
self.distanceBetweenLocations.text = [NSString stringWithFormat:#"%.2f",distanceInMeter] ;
} else {
self.distanceBetweenLocations.text = [NSString stringWithFormat:#"%.f",0.0] ;
}
}
- (IBAction)setFixLocation:(UIButton *)sender {
if(self.locationManager.location != nil){
self.fixedLocation = self.locationManager.location;
self.fixedLocationLatitude.text = [NSString stringWithFormat:#"%.8f", self.fixedLocation.coordinate.latitude];
self.fixedLocationLongitude.text = [NSString stringWithFormat:#"%.8f", self.fixedLocation.coordinate.longitude];
}
}
#end

How to draw route map on Apple map between two annotation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I want to achieve this following route poly line view over Apple map.
And want to show poly line on road, which connect from source to destination.
my code for viewcontroller header file is...
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface TrackViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong,nonatomic) NSMutableArray *arrAnnotation;
#property (nonatomic, retain) MKPolyline *polyLine;
#property (nonatomic, retain) MKPolylineView *polyLineView;
#property (nonatomic, strong) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *currentLocation;
my code for viewcontroller implementation file is...
- (void)viewDidLoad {
[super viewDidLoad];
_mapView.showsUserLocation = YES;
if ([CLLocationManager locationServicesEnabled]) {
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
}
[self.locationManager startUpdatingLocation];
}
NSArray *name=[[NSArray alloc]initWithObjects:
#"Mumbai",
#"Chennai", nil];
self.arrAnnotation=[[NSMutableArray alloc]initWithCapacity:name.count];
MKPointAnnotation *mappin1, *mappin2;
CLLocationCoordinate2D location[3];
mappin1 = [[MKPointAnnotation alloc]init];
location[0] = CLLocationCoordinate2DMake(19.129275,72.905273);
mappin1.coordinate=location[0];
mappin1.title=[name objectAtIndex:0];
[self.arrAnnotation addObject:mappin1];
mappin2 = [[MKPointAnnotation alloc]init];
location[1] = CLLocationCoordinate2DMake(13.063426,80.288086);
mappin2.coordinate=location[1];
mappin2.title=[name objectAtIndex:1];
[self.arrAnnotation addObject:mappin2];
[self.mapView addAnnotations:self.arrAnnotation];
self.mapView.mapType = MKMapTypeStandard;
self.mapView.showsUserLocation = YES;
self.polyLine = [MKPolyline polylineWithCoordinates:location count:2];
[self.mapView setVisibleMapRect:[self.polyLine boundingMapRect]];
[self.mapView addOverlay:self.polyLine];
}
and my out is...
Please help me...
Thanks in advance.
Your location array should not be filled with only two coordinates. Because adding only two coordinates will join the two points together in a straight line. So you need to add more coordinates to your location array in order to have a more accurate polyline.

IOS CLLocation manager returning zero speed

Hi I want to calculate speed within my camera view in XCODE8. The code does not generate error but my speed is returning zero. So I searched the link below :Self Location Manager Delegate error in Xcode 6. My code has two interface files, one for CvVideoCamera and one for ClLocationManager. I never had two interface files in my project, so I am not sure I am doing everything correctly. My code is below, Can you please tell me if something is wrong in my code architecture that causing me to get zero speed.
viewcontroller.h
#interface ViewController : UIViewController<CvVideoCameraDelegate>
#property (nonatomic, strong) CvVideoCamera* videoCamera;
…
#end
#interface LocationController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager*locationManager;
#end
viewcontroller.m
#import "ViewController.h"
int speed;
#interface LocationController() {
}
#end
#implementation LocationController
#synthesize locationManager;
- (void)viewDidLoad
{
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager startUpdatingLocation];
self.locationManager.delegate = (id)self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *loc = locations.lastObject;
loc = locations.lastObject;
speed = loc.speed * 2.236;
}
#end
#interface ViewController (){
….
}
#end
#implementation ViewController
#synthesize imageView;
#synthesize videoCamera
- (void)viewDidLoad
{
[super viewDidLoad];
self.videoCamera.delegate = self;
(void)processImage:(cv::Mat&)image {
NSLog(#"speed: %.d”, speed)}

The Pin is not working

My Map shows a pin on a specific place but it's not showing the pin
Here is the code
WadiRumViewControllerJordan.h
#import <UIKit/UIKit.h>
#include <MapKit/MapKit.h>
#interface WadiRumViewControllerJordan : UIViewController
#property (strong, nonatomic) IBOutlet MKMapView *WadiRumMapView;
#end
WadiRumViewControllerJordan.m
#import "WadiRumViewControllerJordan.h"
#import "WadiRumNSOjectPIN.h"
#interface WadiRumViewControllerJordan ()
#end
//Wadi Rum Coordinates
#define WadiRum_Latitude 29.537355
#define WidiRum_longtitude 35.415026
//Wadi Rum Span
#define WadiRumSpan 0.01f;
#implementation WadiRumViewControllerJordan
#synthesize WadiRumMapView;
- (void)viewDidLoad {
[super viewDidLoad];
//Create WadiRum Region
MKCoordinateRegion WadiRumRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = WadiRum_Latitude;
center.longitude = WidiRum_longtitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta = WadiRum_Latitude;
span.longitudeDelta = WidiRum_longtitude;
WadiRumRegion.center = center;
WadiRumRegion.span = span;
//Set our map
[WadiRumMapView setRegion:WadiRumRegion animated:YES];
//WadiRumNSObjectPIN
//1. Create a coordinate for the use of WadiRum
CLLocationCoordinate2D WadiRumLocation;
WadiRumLocation.latitude = WadiRum_Latitude;
WadiRumLocation.longitude = WidiRum_longtitude;
WadiRumNSOjectPIN * WadiRumAnnitation = [[WadiRumNSOjectPIN alloc] init];
WadiRumAnnitation.coordinate = WadiRumLocation;
WadiRumAnnitation.title = #"Services";
WadiRumAnnitation.subtitle = #"Desert";
{[self.WadiRumMapView addAnnotation:WadiRumAnnitation];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be WadiRumNSOjectPIN
}
#end
WadiRumNSOjectPIN.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface WadiRumNSOjectPIN : NSObject <MKAnnotation>
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString * title;
#property(nonatomic, copy) NSString * subtitle;
#end
WadiRumNSOjectPIN.m
#import "WadiRumNSOjectPIN.h"
#implementation WadiRumNSOjectPIN
#synthesize coordinate;
- (id)initWithLocation:(CLLocationCoordinate2D)coord {
self = [super init];
if (self) {
coordinate = coord;
}
return self;
}
#synthesize coordinate, title, subtitle;
#end
I edited the code above to make it exactly like what I want, I got this error in the picture bellow
In order to conform to MKAnnotation, you must have properties called coordinate, title and subtitle. You've added three extra properties, ttcoordinate, tttitle, and ttsubtitle, but MKAnnotation is going to ignore those, and will look for coordinate, title, and subtitle.
The key reason you're not seeing your annotation is that you're setting ttcoordinate in viewDidLoad. But MKAnnotation will not use that, but rather will refer to the coordinate property you synthesized, but never set. (You do have an initWithLocation method, which suggests you were going to update coordinate, but you never call that.)
Bottom line, I would suggest renaming ttcoordinate, ttitle and ttsubtitle to coordinate, title, and subtitle, and updating all of those references accordingly, and that should fix everything. And you can retire the #synthesize line.

objective-c locationManager as delegate, be notified if location changed

I sourced my locationManager out in a own class and file
Now I want to be notified when the location is updated. So I tried to implement the delegation pattern. But for some reason it does not work. What I did:
In Location.h: specified class, protocol, ivar and property id, delegation method
In Location.m: added a call to the delegation method
In ViewController.h: added delegation protocol
In ViewController.m: implemented the delegation method
Build and run the code. But the delegation method in ViewController.m is not called :(
Any ideas what i missed?
Output
2013-07-04 11:55:30.429 Sandbox2[2001:c07] Inside Lokation::getLocation
2013-07-04 11:55:30.443 Sandbox2[2001:c07] Inside Lokation::locationManager
2013-07-04 11:55:30.449 Sandbox2[2001:c07] currentLat: 51.509980 currentLng -0.133700
// Missing NSLog Statement of the delegate method didFindLocation...
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
// added Lokation header
#import "Lokation.h"
// added LokationDelegate
#interface ViewController : UIViewController <CLLocationManagerDelegate, LokationDelegate>
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) Lokation *lokation;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[self getLocation];
self.lokation = [[Lokation alloc] init];
self.lokation.delegate = self;
[self.lokation getLocation];
}
// added delegate function of Lokation
// Should be run after the locationmanager found a location
-(void)didFindLocation {
NSLog(#"I am found a lokation and I am now in the ViewController");
}
Lokation.h (20130704 updated to working code)
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
// Added class, protocol and method for the delegate
#class Lokation;
#protocol LokationDelegate <NSObject>
-(void)didFindLocation;
#end
#interface Lokation : NSObject <CLLocationManagerDelegate> {
CLLocationDegrees currentLat;
CLLocationDegrees currentLng;
}
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *currentLoc;
// added delegate property
#property (assign, nonatomic) id<LokationDelegate> delegate;
-(void)getLocation;
#end
Lokation.m (20130704 updated to working code)
#import "Lokation.h"
#implementation Lokation
#synthesize locationManager = _locationManager;
#synthesize currentLoc = _currentLoc;
-(void)getLocation {
NSLog(#"Inside Lokation::getLocation");
// active location determination
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
// We don't want to be notified of small changes in location,
// preferring to use our last cached results, if any.
self.locationManager.distanceFilter = 50;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"Inside Lokation::locationManager");
if (!oldLocation ||
(oldLocation.coordinate.latitude != newLocation.coordinate.latitude &&
oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) {
currentLat = newLocation.coordinate.latitude;
currentLng = newLocation.coordinate.longitude;
} else { // oldLocation
currentLat = oldLocation.coordinate.latitude;
currentLng = oldLocation.coordinate.longitude;
}
self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng];
NSLog(#"currentLat: %f currentLng %f", currentLat, currentLng);
// added call to the delegate function
[self.delegate performSelector:#selector(didFindLocation)];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(#"%#", error);
}
Hmm..i think you are doing right but if i do change like below:
remove __unsafe_unretained id<LokationDelegate> delegate; and replace #property (assign, nonatomic) id<LokationDelegate> delegate with #property (retain) id<LokationDelegate> delegate (and synthesize property),it works like charm for me

Resources