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.
Related
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
I am trying to create a NSObject that will get the location and the address, I will also be running some other code. I want to just throw it into my different apps. I am having issues with it updating the location. I have the protocol and delegate working, but I will post all my code to try to make it clear.
In my getLocation.h
#protocol getLocationDelegate
- (void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude;
#end
#interface getLocation : NSObject <CLLocationManagerDelegate> {
CGFloat latitude;
CGFloat longitude;
NSString *address;
}
#property (nonatomic, strong) id<getLocationDelegate> delegate;
#property (nonatomic, retain) CLLocationManager *locationManager;
- (void)getLocationInfo;
In my getLocation.m
- (id)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
return self;
}
- (void)getLocationInfo {
[self.locationManager startUpdatingLocation];
// This is being called but not starting the locationManager
}
After I get the address and the location I call the protocol like this, in my getLocation.h
[self.delegate getLocation:address getLongitude:longitude getLatitude:latitude];
In my .m file that I want to get the location this is the code
getLocation *location = [[getLocation alloc] init];
[location setDelegate:self];
[location getLocationInfo];
Then I have my method when the delegate gets called
-(void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude {
// Do code after location and address has been returned
}
Now my protocol and delegate are working what my problem is I can't get the locationManager to start updating. Not sure whats going on.
I figured it out, ARC was releasing getLocation *location = [[getLocation alloc] init]; before CoreLocation was starting. So I just needed to declare it in my .h and have it be strong.
I have this code below working perfectly for a unique Pin and one annotation. I want to adapt it without too many changes to display more Pins, locations and annotations.
There is a class called MyAnnotationPins with the following lines:
MyAnnotationPins.h
#interface MyAnnotationPins : NSObject < MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly, copy) NSString *title;
#property (nonatomic, readonly, copy) NSString *subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle;
MyAnnotationPins.m
#synthesize coordinate;
#synthesize subtitle;
#synthesize title;
-(id)initWithCoordinate:(CLLocationCoordinate2D)annotCoordinate title:(NSString*)annotTitle subtitle:(NSString*)annotSubtitle
{
self = [super init];
if (self)
{
coordinate = annotCoordinate;
subtitle = [[NSString alloc] initWithString:annotSubtitle];
title = [[NSString alloc] initWithString:annotTitle];
}
return self;
}
And at the view controller the following code:
SecondViewController.h
import "MyAnnotationPins.h"
#interface SecondViewController : UIViewController < MKMapViewDelegate >
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) MyAnnotationPins* biblioAnnotation;
At the SecondViewController.m the implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
MKCoordinateRegion mapRegion;
mapRegion.center.latitude=-18.924129;
mapRegion.center.longitude=-48.283963;
mapRegion.span.latitudeDelta=0.2;
mapRegion.span.longitudeDelta=0.2;
[mapView setRegion:mapRegion animated:YES];
///// This is just for One annotaion/Pin on Map /////
CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
biblioAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:parliamentLocation
title:#"Ponto Biblioteca"
subtitle:#"Taxi proximo"];
[mapView addAnnotation:biblioAnnotation];
If you want more than one pin and annotaion copy the CLLocation instance below and change the following atributes
CLLocationCoordinate2D secondLocation = CLLocationCoordinate2DMake(another latitude, another longitude);
secondAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:secondLocation
title:#"Second Title"
subtitle:#"Second subtitle"];
[mapView addAnnotation:secondAnnotation]; <code>
And so on for the third, fourth fifth etc. Do not forget to create the secondLocation proerty at your view controller like the first one in SecondViewController.h and also
#synthesize secondAnnotation property at SecondViewController.m file
#property (strong, nonatomic) MyAnnotationPins* secondAnnotation;
Add them in a loop like so
for(X in Y)
{
CLLocationCoordinate2D parliamentLocation = CLLocationCoordinate2DMake(-18.924129, -48.283963);
MyAnnotationPins* biblioAnnotation = [[MyAnnotationPins alloc]
initWithCoordinate:parliamentLocation
title:#"Ponto Biblioteca"
subtitle:#"Taxi proximo"];
[mapView addAnnotation:biblioAnnotation];
}
As you loop through your list you can get the right coordinates and title for each one. This way you list can grow or shrink and you won't need to add third, fourth or fifth annotation properties.
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;
I am trying to put MKAnnotations on MKMapView. I am following the procedure that has been suggested on various available tutorials. I have created PlaceMark.h/.m file from NSobject Class as following.
PlaceMark.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
#interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
#property (nonatomic,copy) NSString *title;
#property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
#end
PlaceMark.m
#import "PlaceMark.h"
#implementation PlaceMark
#synthesize coordinate,title;
-(void)dealloc
{
[title release];
[super dealloc];
}
#end
in my viewController which holds MKMapView, in viewDidload I have following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;
[parkingMap setRegion:region animated:YES];
PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.
ann.title=#"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}
Can anyone please tell me what I am doing wrong? BTW I am using Xcode4.2 with iOS sdk5.0 and
Not using storyboards/automatic reference counting
Thanks
Sumit
You've defined the coordinate property as readonly so you can't set it.
Change it to readwrite.
Also, if you just need a basic annotation object with a title, subtitle, and settable coordinate, you can use the built-in MKPointAnnotation class instead of defining your own class. The creation and initialization would be identical (just replace your class name with MKPointAnnotation).