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)
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'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.
I'm having trouble changing the pinColor for an annotation in MapKit. When I don't try to implement the mapView:viewForAnnotation: method everything works (the annotation is added) but when I try to change the annotation view, the simulator crashes :
Here is the code :
MapViewController.h
#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "Annotation.h"
#implementation MapViewController
#synthesize myMapView;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D location;
location.longitude = 2.21;
location.latitude = 48.5;
MKCoordinateSpan span;
span.latitudeDelta = 1*(1 - 0);
span.longitudeDelta = 1*(1 - 0);
MKCoordinateRegion region;
region.span = span;
region.center = location;
[myMapView setRegion:region animated:NO];
[myMapView regionThatFits:region];
Annotation *someAnnotation =[[Annotation alloc] init];
[myMapView addAnnotation:someAnnotation];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation {
MKPinAnnotationView *customPinview = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
customPinview.pinColor = MKPinAnnotationColorGreen;
customPinview.animatesDrop = YES;
customPinview.canShowCallout = YES;
return customPinview;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
MapViewController.m
//
// MapViewController.h
// TestMap
//
// Created by Johan Ismael on 10/21/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface MapViewController : UIViewController<MKMapViewDelegate> {
#private
IBOutlet MKMapView *myMapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *myMapView;
//- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id<MKAnnotation>) annotation;
#end
Thanks in advance !!!
Note : MapViewController is declared as the delegate of the MapView in IB
In the viewForAnnotation method, the alloc is being done on MKAnnotationView instead of MKPinAnnotationView. It must be crashing with "unrecognized selector" because MKAnnotationView doesn't have a pinColor property. Change the alloc to:
MKPinAnnotationView *customPinview = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:nil] autorelease];