Map View Viewing Issues - ios

I have my iPhone application that loads a map view, my problem is that it loads the map view zeroed in on one single map pin, I want it to load in a more holistic view that shows the whole map of the United States right when the app is launched. Is there a way to execute this? Here is my code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController
{
MKMapView *mapView;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
-(IBAction)SetMap:(id)sender;
-(IBAction)GetLocation:(id)sender;
-(IBAction)Directions:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "MapPin.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
//Moss Preserve coordinates
MKCoordinateRegion MossPreserveRegion = { {0.0, 0.0}, {0.0, 0.0}};
MossPreserveRegion.center.latitude = 33.3816566;
MossPreserveRegion.center.longitude = -86.8415451;
MossPreserveRegion.span.longitudeDelta = 0.01f;
MossPreserveRegion.span.latitudeDelta = 0.01f;
[mapView setRegion:MossPreserveRegion animated:YES];
//Moss Preserve annotation and map pin
MapPin *MossPreserveAnnotation = [[MapPin alloc] init];
MossPreserveAnnotation.title = #"Moss Rock Preserve Boulder Fields";
MossPreserveAnnotation.subtitle = #"Preserve Pkwy, Hoover, AL";
MossPreserveAnnotation.coordinate = MossPreserveRegion.center;
[mapView addAnnotation:MossPreserveAnnotation];
//Horse Pens 40 coordinates
MKCoordinateRegion HorsePenRegion = { {0.0, 0.0}, {0.0, 0.0}};
HorsePenRegion.center.latitude = 33.9207535;
HorsePenRegion.center.longitude = -86.3089447;
HorsePenRegion.span.longitudeDelta = 0.01f;
HorsePenRegion.span.latitudeDelta = 0.01f;
[mapView setRegion:HorsePenRegion animated:YES];
//Horse Pens 40 annotation and map pin
MapPin *HorsePenAnnotation = [[MapPin alloc] init];
HorsePenAnnotation.title = #"Horse Pens 40";
HorsePenAnnotation.subtitle = #"Steele, AL ";
HorsePenAnnotation.coordinate = HorsePenRegion.center;
[mapView addAnnotation:HorsePenAnnotation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)SetMap:(id)sender;
{
switch (((UISegmentedControl *) sender).selectedSegmentIndex)
{
case 0:
mapView.mapType = MKMapTypeStandard;
break;
case 1:
mapView.mapType = MKMapTypeSatellite;
break;
case 2:
mapView.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)GetLocation:(id)sender;
{
mapView.showsUserLocation = YES;
}
-(IBAction)Directions:(id)sender;
{
NSString *urlString = #"http://maps.apple.com/maps?daddr=33.3816566,-86.8415451";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
#end
MapPin.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapPin : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic, assign) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#end
MapPin.m
#import "MapPin.h"
#implementation MapPin
#synthesize coordinate, title, subtitle;
#end
Thank you for any advice

You need to set the zoom level of an MKMapView.
Better look at this:
http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/

Related

MapView Application

I have written simple mapView application.It is showing two kind of problems.
1- Execution is stopping on Thread 1: signal SIGABRT error.
2- This function is showing compile time error .
- (IBAction)findMe:(id)sender {
If( [[toggleButton titleForState:UIControlStateNormal] isEqualToString:#"Find Me"] )
{
[toggleButton setTitle:#"Hide Me" forState:UIControlStateNormal];
mapView.showsUserLocation=YES;
}
else
{
[toggleButton setTitle:#"Find Me" forState:UIControlStateNormal];
mapView.showsUserLocation=NO;
}
}
How to remove these error ?
I want put coordinates of more then one location in my code i want to show loc.png icon on map corresponding to those coordinates how i can accomplish this task?
You can see the sample project from this link: https://drive.google.com/open?id=0B5pNDpbvZ8SnRmNFS0pjVnJFWHc
You are getting compile time error for 3 reasons:
1) The "If" you are using in if-else condition should be "if" i.e lowercase.
2). You have an IBOutlet connect to findMe button in storyboard which do not exist in your view controller. So either remove it or add it.
3)You are using MKMapView but you didn't added the MapKit Framework in "Link Binaries" in build phases option of your project.
Please do all these steps to have your code compiled & executed error free.
Answer to your query:
I want put coordinates of more then one location in my code i want to show loc.png icon on map corresponding to those coordinates how i can accomplish this task?
Here is the code for ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate>
#end
Here is the code for ViewController.m
#import "ViewController.h"
#import "MyAnnotation.h"
#import <MapKit/MapKit.h>
#interface ViewController ()
#property (strong, nonatomic) IBOutlet MKMapView *myMapView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// setup the map view, delegate and current location
[self.myMapView setDelegate:self];
self.myMapView.mapType = MKMapTypeStandard;
CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(25.085130,-77.331428);
[self.myMapView setCenterCoordinate:myLocation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myLocation, 2000, 2000);
region.center = self.myMapView.centerCoordinate;
self.myMapView.showsUserLocation = YES;
[self.myMapView setRegion:region animated:YES];
[self dropPins];
}
-(void)dropPins {
NSMutableArray *annotationArray = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(25.085130, -77.331428);
MyAnnotation *annotation1 = [[MyAnnotation alloc] initWithCoordinates:location1 image:#"loc.png"];
[annotationArray addObject:annotation1];
[self.myMapView addAnnotations:annotationArray];
[annotationArray removeAllObjects];
CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(25.085130, -77.336428);
MyAnnotation *annotation2 = [[MyAnnotation alloc] initWithCoordinates:location2 image:#"loc.png"];
[annotationArray addObject:annotation2];
[self.myMapView addAnnotations:annotationArray];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *identifier = #"MyLocation";
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else
{
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
if([[(MyAnnotation *)annotationView.annotation image] isEqualToString:#"pin1.png"])
annotationView.image = [UIImage imageNamed:#"loc.png"];
if([[(MyAnnotation *)annotationView.annotation image] isEqualToString:#"pin2.png"])
annotationView.image = [UIImage imageNamed:#"loc.png"];
return annotationView;
}
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Add new files MyAnnotation.h/MyAnnotation.m
Here is the code for MyAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyAnnotation : NSObject <MKAnnotation>
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy, readonly) NSString *image;
-(id)initWithCoordinates:(CLLocationCoordinate2D) paramCoordinates
image:(NSString *) paramImage;
#end
Here is the code for MyAnnotation.m
#import "MyAnnotation.h"
#implementation MyAnnotation
-(id)initWithCoordinates:(CLLocationCoordinate2D)paramCoordinates
image:(NSString *)paramImage
{
self = [super init];
if(self != nil)
{
_coordinate = paramCoordinates;
_image = paramImage;
}
return (self);
}
#end

Track a device and display data with annotations in IOS

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

trying to pass text from tableview to a view with a map

I have a UITableView that opens a view with a map, i called Mapa class. I am having problems passing any kind of text information from this table view to the map. I need to send a string text to be the title of my map, and the coordinates of a CLLocation.
Here is part of the code
MyTableView.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
instMapa = [[Mapa alloc] initWithNibName:#"Mapa" bundle:nil];
instMapa.mytext = #"pass text to be the title";
[self.navigationController pushViewController:instMapa animated:YES];
}
Mapa.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#class MapViewAnnotation;
#interface Mapa : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *mapView;
NSString *stringTitle;
NSString *mytext;
MapViewAnnotation *newAnnotation;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapView;
#property (nonatomic, retain) NSString *stringTitle;
#property (nonatomic, retain) NSString *mytext;
#property (nonatomic, retain) MapViewAnnotation *newAnnotation;
#end
Mapa.m
#import "Mapa.h"
#import "MapViewAnnotation.h"
#implementation Mapa
#synthesize mapView;
#synthesize stringTitle;
#synthesize mytext;
#synthesize newAnnotation;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D location;
location.latitude = (double) 51.501468;
location.longitude = (double) -0.141596;
// Add the annotation to our map view
newAnnotation = [[MapViewAnnotation alloc] initWithTitle:mytext andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
}
// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
[mv setRegion:region animated:YES];
[mv selectAnnotation:mp animated:YES];
}
// Received memory warning
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
// If the view unloads, release the map view
- (void)viewDidUnload {
}
- (void)dealloc {
[newAnnotation release];
[mytext release];
[stringTitle release];
[mapView release];
[super dealloc];
}
#end
MapViewAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d;
#end
MapViewAnnotation.m
#implementation MapViewAnnotation
#synthesize title, coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d {
[super init];
title = ttl;
coordinate = c2d;
return self;
}
- (void)dealloc {
[title release];
[super dealloc];
}
#end
Thanks for the help!
From your question, I understand that; you need to pass the string value of the selected table view cell to your map view.
For that you need to write this code in your didSelectRowAtIndexPath,
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
instMapa.mytext = cell.textLabel.text;

MapView Set Location

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

Removing MKMapView Annotations causes leaks

I've boiled a very complex set of web services and searches down to the simple following code.
I need to be able to add annotations to a map in response to a search (or in the sample below to the click of a button), then allow the user to click the button again and get a new set of results. In reality there will be a different number, but in the simplified example, we always add one annotation to the mapview.
I believe my code should remove the existing annotations and add a new one, but it leaks 32 bytes on second and subsequent button pushes.
What am I missing ? (Or retaining as the case may be!)
testViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "MyMark.h"
#interface testViewController : UIViewController {
MKMapView *mapView;
}
#end
testViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
self.title=#"test";
}
return self;
}
- (void) storeLocationInfo: (CLLocationCoordinate2D) loc title:(NSString *)t subtitle:(NSString *)st index:(int)i {
NSArray * annotations = [mapView annotations];
[mapView removeAnnotations:annotations];
MyMark * mymark=[[MyMark alloc] initWithCoordinate:loc];
[mapView addAnnotation:mymark];
[MyMark release];
}
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Add point to map" style:UIBarButtonItemStylePlain target:self action:#selector(addPushed)];
[self.navigationItem setRightBarButtonItem:barButton];
[barButton release];
mapView=[[MKMapView alloc] initWithFrame:CGRectMake(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
mapView.showsUserLocation=FALSE;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];
[mapView release];
}
- (void) addPushed {
MKCoordinateRegion reg = mapView.region;
[self storeLocationInfo:reg.center title:#"price" subtitle:#"title" index:1];
}
- (void)dealloc {
[super dealloc];
}
MyMark.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MyMark : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
int index;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, readonly) int index;
#property (nonatomic, retain) NSString * title;
#property (nonatomic, retain) NSString * subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) coordinate;
-(id)setCoordinate:(CLLocationCoordinate2D) coordinate;
-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int)i ;
#end
MyMark.m
#import "MyMark.h"
#implementation MyMark
#synthesize coordinate, index;
#synthesize title,subtitle;
-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
-(id)setCoordinate:(CLLocationCoordinate2D) c{
coordinate=c;
NSLog(#"%f,%f",c.latitude,c.longitude);
return self;
}
-(id)setTitle:(NSString *)t subtitle:(NSString *)st index:(int) i{
self.title=t;
self.subtitle=st;
index=i;
return self;
}
-(void) dealloc {
[title release];
[subtitle release];
[super dealloc];
}
You're not releasing mymark in storeLocationInfo:title:subtitle:index:. It looks like the problem is a typing error. The line that reads
[MyMark release];
should be
[mymark release];
Note the case difference. The first line sends release to the class, not the instance.

Resources