the map could not show my location - ios

I want to display a map and show the location. My code is running well, but the location could not be displayed on the map. I do not know how to do it.
Well here are my classes:
ViewController.h
#interface ViewController :
UIViewController<AGSMapViewLayerDelegate,CLLocationManagerDelegate>
{
AGSMapView *_mapView;
AGSGraphicsLayer *_graphicsLayer;
AGSTiledMapServiceLayer *_tiledLayer;
CLLocationManager *locationManager;
CLLocation *startingPoint;
}
#property (nonatomic,retain) IBOutlet AGSMapView *mapView;
#property (nonatomic, retain) AGSTiledMapServiceLayer *tiledLayer;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) AGSGraphicsLayer *graphicsLayer;
#property (nonatomic, retain) CLLocation *startingPoint;
ViewController.m
#interface ViewController()
#end
#implementation ViewController
#synthesize mapView = _mapView;
#synthesize graphicsLayer = _graphicsLayer;
#synthesize tiledLayer = _tiledLayer;
#synthesize locationManager;
#synthesize startingPoint;
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.layerDelegate =self;
self.graphicsLayer = [AGSGraphicsLayer graphicsLayer];
[self.mapView addMapLayer:self.graphicsLayer withName:#"graphicsLayer"];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send loc updates to myself
self.locationManager.distanceFilter = 1000.0f; // 1 kilometer
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
}
- (void)mapViewDidLoad:(AGSMapView *)mapView{
[self.locationManager startUpdatingLocation]; //启用位置监控
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
self.startingPoint = [locations lastObject];
[self.locationManager stopUpdatingLocation]; //获取位置信息
CGPoint coord;
coord.x = startingPoint.coordinate.longitude;
coord.y = startingPoint.coordinate.latitude;
//将坐标用AGSPoint来表示
AGSPoint *mappoint = [[AGSPoint alloc]initWithX:coord.x y:coord.y spatialReference:nil];
//[self.graphicsLayer removeAllGraphics];
AGSPictureMarkerSymbol *pt;
pt = [AGSPictureMarkerSymbol pictureMarkerSymbolWithImageNamed:#"LocationDisplay.png"];
AGSGraphic *LocationDisplay = [[AGSGraphic alloc] initWithGeometry:mappoint symbol:pt attributes:nil infoTemplateDelegate:nil];
// 添加要素到GraphicsLayer
[self.graphicsLayer addGraphic:LocationDisplay];
[self.graphicsLayer refresh];
//漫游到指定级别
[self.mapView centerAtPoint:mappoint animated:YES];
int levelToZoomTo = 12;
AGSLOD* lod = [self.tiledLayer.mapServiceInfo.tileInfo.lods objectAtIndex:levelToZoomTo];
float zoomFactor = lod.resolution/self.mapView.resolution;
AGSMutableEnvelope *newEnv = [AGSMutableEnvelope envelopeWithXmin:self.mapView.visibleAreaEnvelope.xmin
ymin:self.mapView.visibleAreaEnvelope.ymin
xmax:self.mapView.visibleAreaEnvelope.xmax
ymax:self.mapView.visibleAreaEnvelope.ymax
spatialReference:self.mapView.spatialReference];
[newEnv expandByFactor:zoomFactor];
[self.mapView zoomToEnvelope:newEnv animated:YES];
}

You need to provide a spatial reference when you create the AGSPoint from the CLLocation coordinates. Use [AGSSpatialReference wgs84SpatialReference]
If the map is not using WGS84 spatial reference then you need to reproject the point to the map spatial reference before you add it to the map using the geometry engine.
OR
you can use the locationDisplay property built into AGSMapView, by setting mapView.locationDisplay.startDataSource to display your current location.
See the docs for AGSMapView

func mapViewDidLoad(mapView: AGSMapView!) {
//do something now that the map is loaded
//for example, show the current location on the map
mapView.locationDisplay.startDataSource()
}

Try setting mapview.showUserLocation = YES ; or set it in XIB

Related

Zoom with animation in current location when map loads in IOS

I have used map in my view controller but when the maps loads it shows me view of the whole country. I want to show my current location view when maps loads, for that I have used didUpdateUserLocation method and set delegate for it.
My code is this:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES];
}
Now when my map loads it animate and zoom but in wrong location. It is taking me to any location in sea when map loads , but my current location is different.
I have also tested it in real device but it does not show my location.
The maps looks like that when loads:
ViewController class code:
#property BOOL updateUserLocation;
#property(strong,nonatomic) CLLocation *userLocation;
#property (nonatomic, retain) MKPolyline *routeLine;
#property (nonatomic, retain) MKPolylineView *routeLineView;
#property (nonatomic,retain) NSString *latitude;
#property (nonatomic,retain) NSString *longitude;
- (void)viewDidLoad {
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
_AlertView.hidden=YES;
_distanceView.hidden=YES;
self.mapView.zoomEnabled=YES;
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[self.mapView setShowsUserLocation:YES];
[self.mapView setDelegate:self];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES];
}
Add these lines in the viewDidLoad:
self.mapView.showsUserLocation = YES;
if you want to center the map to the user location add this:
[self.mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
EDIT:
ViewController example:
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController () <CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (nonatomic, strong) CLLocationManager *locationManager;
#property (nonatomic, strong) CLLocation *location;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[self mapView] setShowsUserLocation:YES];
self.locationManager = [[CLLocationManager alloc] init];
// we have to setup the location manager with permission in later iOS versions
if ([[self locationManager] respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[[self locationManager] requestWhenInUseAuthorization];
}
[[self locationManager] setDelegate:self];
[[self locationManager] setDesiredAccuracy:kCLLocationAccuracyBest];
[[self locationManager] startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// Stop updating location, when you want to restart update use [[self locationManager] startUpdatingLocation];
[manager stopUpdatingLocation];
self.location = locations.lastObject;
// zoom the map into the users current location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance
(self.location.coordinate, 2, 2);
[[self mapView] setRegion:viewRegion animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I've only added the IBOutlet on the storyboard and the NSLocationWhenInUseUsageDescription key in the plist.
If you change the location on the simulator you can see the maps change location.
If you want I can upload the complete example project.
NEW EDIT:
There is an error in you code!
When you use [self.mapView setRegion:MKCoordinateRegionMake(_userLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)) animated:YES], you use _userLocation that is your CLLocation variable! You must use userLocation that is the value that you receive from the method.
I recommend you to change the CCLocation variable name and use the userLocation name only for the variable used in the didUpdateUserLocation method.
You can call this method and zoom on map
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = ; // your latitude value
zoomLocation.longitude= // your longitude value
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.18; // change as per your zoom level
span.longitudeDelta=0.18;
region.span=span;
region.center= zoomLocation;
[mapview setRegion:region animated:TRUE];
[mapview regionThatFits:region];
Use didUpdateToLocation instead of didUpdateUserLocation.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
MKCoordinateRegion region;
region.span.latitudeDelta = 0.005;
region.span.longitudeDelta = 0.005;
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
[self.mapView setRegion:region animated:YES];
}
See also my library.
https://github.com/koogawa/KGWLocationPicker/blob/master/KGWLocationPicker/KGWLocationPickerViewController.m

UISearchBar not working even with delegate set in viewDidLoad

The searchBarBookmarkButtonClicked method isn't firing when I tap on the search bar. I have already added the self.searchBar.deleagate = self; and in my .h file I added the <UISearchBarDelegate>.
.h file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet MKMapView *myMap;
#end
.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.searchBar.delegate = self;
self.myMap.delegate = self;
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{
NSLog(#"working");
[self.searchBar resignFirstResponder];
NSLog(#"working");
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.searchBar.text];
[self.myMap addAnnotation:annotation];
MKMapRect mr = [self.myMap visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y -mr.size.width * 0.25;
[self.myMap setVisibleMapRect:mr animated:YES];
}];
}
#end
Make sure you have
All delegate being set properly, this can be done either in code or
in storyboard.
Make use of correct delegate method to capture the
event.
If you think
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar
then try using the other method which is more straight forward to trigger search
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
Try this UISearchBar delegate method:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
Thank you #Rahul! Turns out I was using the wrong method!
.h:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) IBOutlet MKMapView *myMap;
#end
.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.searchBar.delegate = self;
self.myMap.delegate = self;
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar * )searchBar{
[self.searchBar resignFirstResponder];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:self.searchBar.text completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
CLLocationCoordinate2D newLocation = [placemark.location coordinate];
region.center = [(CLCircularRegion *)placemark.region center];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:newLocation];
[annotation setTitle:self.searchBar.text];
[self.myMap addAnnotation:annotation];
MKMapRect mr = [self.myMap visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y -mr.size.width * 0.25;
[self.myMap setVisibleMapRect:mr animated:YES];
}];
}
#end

how to show a map with a single annotation and the users location and have the map display both whether the user is 100ft or 100miles

I'd like to have a map with just two points, the users location and a single annotation (it's for a pov). I have included what I
have below. Ideally, I would like it to handle if the user is 100 feet away or 100 miles in displaying both the User and the annotation in
the map view. I think it would be best if the location were at the center and the user were on the edge with some reasonable 20% buffer on the edge of the map view. I only need to support iOS 7 and greater.
Is there something like sizeToFit for annotations that takes into account users position as an annotation?
my MapViewController.h
#interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
my MapViewController.m:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#import "MapViewController.h"
#import "MYUtility.h"
#interface MapViewController (){
NSMutableArray *_yourAnnotationArray;
MKPointAnnotation *_locationPoint;
BOOL _firstTime;
}
#end
#implementation MapViewController
- (void)viewDidLoad {
[super viewDidLoad];
_firstTime=YES;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
if(IS_OS_8_OR_LATER) {
//[self.locationManager requestWhenInUseAuthorization];
//[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startUpdatingLocation];
}
[self.locationManager startUpdatingLocation];
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
CGFloat latitude=MYLocationLatitude();
CGFloat longitude=MYLocationLongitude();
_locationPoint = [[MKPointAnnotation alloc] init];
_locationPoint.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
_locationPoint.title = #"Where am I?";
_locationPoint.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:_locationPoint];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//if(_firstTime){
NSLog(#"here is my thinking %#", [locations lastObject]);
[self setMapStart];
}
-(void)setMapStart
{
//if(_firstTime){
NSLog(#"at top of _firstTime being true");
MKPointAnnotation *myPoint = [[MKPointAnnotation alloc] init];
myPoint.coordinate = CLLocationCoordinate2DMake(34.035645, -118.233434);
MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
//[_yourAnnotationArray addObject:annotationPoint];
_yourAnnotationArray=[[NSMutableArray alloc] initWithObjects:_locationPoint,annotationPoint, nil];
//NSLog(#"here is my count: %i",(unsigned long)[_yourAnnotationArray count]);
[self.mapView showAnnotations:self.mapView.annotations animated:YES]; // <- determine when this has run
_firstTime=NO;
//}
}

Using MKOverlayRenderer in iOS7 to display UIImage

I am trying to display a UIImage as a overlay for a map-based iPad application. I attempted to follow the 4 steps to setting a custom overlay defined in Apple's documentation: define the overlay data object, define the object renderer, implement the map view in map view delegate, and add overlay data object to to map view. After attempting to do all these, there is no overlay when I run the simulator. Any advice would be fantastic. My code is as follows
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "bjorkOverlay.h"
#import "bjorkOverlayRenderer.h"
#interface TUTViewController : UIViewController <MKMapViewDelegate>
{
CLLocationManager *locationManager;
CLLocation *location;
}
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property(nonatomic) MKCoordinateRegion region;
#property (nonatomic, readonly) MKMapRect boundingMapRect;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly) CLLocationCoordinate2D bjorkOverlayCenterPoint;
#property (nonatomic, readonly) MKMapRect bjorkOverlayBoundingMapRect;
#end
ViewController.m
#import "TUTViewController.h"
#import "bjorkOverlay.h"
#import "bjorkOverlayRenderer.h"
#import MapKit;
#interface TUTViewController () <MKMapViewDelegate>
#end
#implementation TUTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake( 45.022943 ,-87.146606);
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake( 45.048513, -87.124765);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
[self.mapView setVisibleMapRect:mapRect animated:YES];
bjorkOverlay * mapOverlay = [[bjorkOverlay alloc] init];
[_mapView addOverlay:mapOverlay];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay: (id<MKOverlay>)overlay {
MKOverlayRenderer *mapOverlay = [[MKOverlayRenderer alloc] init];
return mapOverlay;
}
// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
bjorkOverlay.h
#import <Foundation/Foundation.h>
#import MapKit;
#interface bjorkOverlay : NSObject <MKOverlay>
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate;
#property (nonatomic, assign) CLLocationCoordinate2D bjorkOverlayCenterPoint;
#property (nonatomic) MKMapRect bjorkOverlayBoundingMapRect;
#end
bjorkOverlay.m
#import "bjorkOverlay.h"
#import MapKit;
#implementation bjorkOverlay
+ (id)overlayWithCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius{
bjorkOverlay* overlay = [[bjorkOverlay alloc] init];
overlay.coordinate = coordinate;
return overlay;
}
-(MKMapRect)boundingMapRect{
CLLocationCoordinate2D lowerLeftCoord = CLLocationCoordinate2DMake( 45.022943 ,-87.146606);
MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoord);
CLLocationCoordinate2D upperRightCoord = CLLocationCoordinate2DMake( 45.048513, -87.124765);
MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoord);
MKMapRect mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
return mapRect;
}
- (CLLocationCoordinate2D)bjorkOverlayCenterPoint
{
CLLocationCoordinate2D coord1 = {
45.035728,-87.1356855
};
return coord1;
}
#end
bjorkOverlayRenderer.h
#import <MapKit/MapKit.h>
#interface bjorkOverlayRenderer : MKOverlayRenderer
#end
bjorkOverlayRender.m
#import "bjorkOverlayRenderer.h"
#implementation bjorkOverlayRenderer
- (void)drawMapRect:(MKMapRect)mapRect
zoomScale:(MKZoomScale)zoomScale
inContext:(CGContextRef)ctx
{
UIImage *image = [UIImage imageNamed:#"overlay.png"];
CGImageRef imageReference = image.CGImage;
MKMapRect theMapRect = [self.overlay boundingMapRect];
CGRect theRect = [self rectForMapRect:theMapRect];
CGRect clipRect = [self rectForMapRect:mapRect];
CGContextAddRect(ctx, clipRect);
CGContextClip(ctx);
CGContextDrawImage(ctx, theRect, imageReference);
}
#end
Any advice would be great or if you have seen sample code for MKOverlayRenderer I would appreciate a link. Thanks

How does locationManager get coordinates?

I can't understand how CLLocationManager get my current location coordinates ? if I don't call [locationManager startUpdatingLocation] and don't set parameters in locationManager.location.coordinate.
ViewController.h
#interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
#property (weak, nonatomic) IBOutlet MKMapView* mapView;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
-(void) mapView:(MKMapView *) mv didAddAnnotationViews:(NSArray *)views {
MKCoordinateRegion region;
region = MKCoordinateRegionMakeWithDistance(locationManager.location.coordinate, 1000, 1000);
[mv setRegion:region animated:YES];
}
Most likely it's a cached value after MapView requested current location update because of the following line:
[self.mapView setShowsUserLocation:YES];

Resources