I'm trying to open the map with a given set of co ordinates. It isn't working. It works only if I exit the view and enter it again.
Here is my code -
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface mapViewController : UIViewController <MKMapViewDelegate>
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#end
mapViewController.m
#import "mapViewController.h"
#interface mapViewController ()
#end
#implementation mapViewController
#synthesize mapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(zoomLocation, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}
#end
Sometimes I have found that methods that animate the screen do not work as expected when running at the same time as view controller transitions. Try moving the code to zoom the map to viewDidAppear.
Related
I am new in objective c.I have created a project which consist of a mapView
In ViewController.h in ny project,
#import <UIKit/UIKit.h>
#import "MapKit/MapKit.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
In ViewController.m file i have viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
// Do any additional setup after loading the view, typically from a nib.
}
I want to give coordinates of more then one location in my code, and I want to show loc.png icon on map corresponding to those coordinates. how can I accomplish this task? And how to zoom the map to maximum scale ?
Form this link you can download my sample project: https://drive.google.com/file/d/0B5pNDpbvZ8SnZGZnU1ZfbjZMRWs/view?usp=sharing
I had working on your question and this are my results, use this code, now Iam using a custom class that implements the MKAnnotation protocol
EDITED
Place.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface Place : NSObject<MKAnnotation>
#property (nonatomic) CLLocationCoordinate2D coordinate;
// Title and subtitle for use by selection UI.
#property (nonatomic, nullable) NSString *title;
#property (nonatomic, nullable) NSString *subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name;
#end
Place.m
#import "Place.h"
#implementation Place
#synthesize coordinate,title,subtitle;
-(id)initWithCoordinates:(CLLocationCoordinate2D) coordinates andName:(NSString*)name
{
self = [super init];
if(self)
{
[self setTitle:name];
[self setCoordinate:coordinates];
}
return self;
}
#end
Modified Code
#import "ViewController.h"
#import "Place.h"
#interface ViewController () <MKMapViewDelegate>
#property NSMutableArray * arrayOfLocations;
#end
#implementation ViewController
#synthesize mapView;
- (void)viewDidLoad {
[super viewDidLoad];
mapView.showsUserLocation=YES;
self.arrayOfLocations = [NSMutableArray arrayWithObjects:[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(40.416691, -3.700345) andName:#"MADRID"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -3.700345) andName:#"SOMEWARE IN THE MAP"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(35.416691, -40.700345) andName:#"SOMEWARE IN THE MAP1"],
[[Place alloc] initWithCoordinates:CLLocationCoordinate2DMake(20.416691, -50.700345) andName:#"SOMEWARE IN THE MAP2"], nil];
[self.mapView setDelegate:self];
[self.mapView addAnnotations:self.arrayOfLocations];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView * annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:#"testAnnotationView"];
if(annotationView == nil){
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"testAnnotationView"];
annotationView.image = [UIImage imageNamed:#"loc.png"];
annotationView.canShowCallout = true;
}
return annotationView;
}
#end
Hope this helps you,
This is how it looks
I would like to store coordinates with additional data according to the gps locations and view the coordinates in a map with pin annotations and the additional data in Callouts (Title, Subtitle). Ive already managed to get my data from my device to my iPhone via Bluetooth. I get about every second new coordinates but my device is very slow, so i only need so save the coordinates and the additional data every 10 meters. Im new in iOS programming and Im looking forward to your help! ;)
Here is my Code:
ViewController.m
#interface SecondViewController ()
#end
extern float lat;
extern float lon;
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGradients];
_mapView.showsUserLocation = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (lat > 0) {
double pointOneLatitude = lat;
double pointOneLongitude = lon;
CLLocationCoordinate2D pointOneCoordinate = {pointOneLatitude, pointOneLongitude};
KTMapAnnotation *pointOneAnnotation = [[KTMapAnnotation alloc] initWithCoordinate:pointOneCoordinate];
[pointOneAnnotation setTypeOfAnnotation:PIN_ANNOTATION];
[self.mapView addAnnotation:pointOneAnnotation];
}
}
ViewController.h
import <UIKit/UIKit.h>
import <MapKit/MapKit.h>
import "KTMapAnnotation.h"
#interface SecondViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
KTMapAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface KTMapAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D _coordinate;
}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
// 08 - Add a Callout
- (NSString*) title;
- (NSString*) subtitle;
#property(nonatomic, strong) NSString *typeOfAnnotation;
#end
KTMapAnnotation.m
#import "SecondViewController.h"
#import "KTMapAnnotation.h"
#implementation KTMapAnnotation
#synthesize coordinate=_coordinate;
#synthesize typeOfAnnotation;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate
{
self = [super init];
if (self != nil)
{
_coordinate = coordinate;
}
return self;
}
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
_coordinate = newCoordinate;
}
// Add a Callout
- (NSString*) title
{
return #"Title“;
}
// Add a Callout
- (NSString*) subtitle
{
return #„subtitel";
}
#end
When i compile and run my app the map view doesnt zoom to location as i though it would
.h file contains the following
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface myContactUsViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *mapView;
#end
.m file contains the following
#import "myContactUsViewController.h"
#define METERS_PER_MILE 1609.344
#interface myContactUsViewController ()
#end
#implementation myContactUsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
// 1
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
[_mapView setRegion:viewRegion animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.18;
span.longitudeDelta=0.18;
region.span=span;
region.center= zoomLocation;
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];
I have an MKMapView on my storyboard and when i go to the storyboard just a default view loads not the specifications have set lat and long and span but doesnt zoom to the location im looking for.
here is .h
//
// ContactViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ContactViewController : UIViewController
#property (weak, nonatomic) IBOutlet MKMapView *myMapView;
#end
here is .m
//
// ContactViewController.m
#import "ContactViewController.h"
#interface ContactViewController ()
#end
// define long and lat of location
#define map_long 42.877391;
#define map_lat -80.734766;
// define span
#define map_span 0.05f;
#implementation ContactViewController
#synthesize myMapView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
//[super viewDidLoad];
MKCoordinateRegion myregion;
CLLocationCoordinate2D mycenter;
mycenter.latitude = map_lat;
mycenter.longitude = map_long;
MKCoordinateSpan myspan;
myspan.latitudeDelta = map_span;
myspan.longitudeDelta = map_span;
myregion.center = mycenter;
myregion.span = myspan;
// Do any additional setup after loading the view.
[myMapView setRegion:myregion animated:YES];
[myMapView setCenterCoordinate:mycenter animated:YES];
}
#end
You're not assigning anything to myCenter .
You should be creating your region with something like:
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(yourCenterCoord, 250, 250);
(where the first parameter is your CLLocationCoordinate2D center variable and the other two are latitude and longitude distance from center)
Can anyone help me with a problem I have with a mapView application that is only displaying the default location and not the specific location I have specified. My code is as follows. I'm learning Xcode so have probably made a schoolboy error :-
.H
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface RavenMapViewController : UIViewController <MKMapViewDelegate> {
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(void) goLocation;
#end
.M
#import "RavenMapViewController.h"
#implementation RavenMapViewController
#synthesize mapView;
-(void) goLocation
{
MKCoordinateRegion newRegion;
newRegion.center.latitude = 39.278112;
newRegion.center.longitude = -76.622772;
newRegion.span.latitudeDelta = 0.008388;
newRegion.span.longitudeDelta = 0.016243;
[self.mapView setRegion:newRegion animated:YES];
}
- (void)viewDidLoad
{
[self goLocation];
}
- (void)viewDidUnload
{
}
#end