How to create an MKMapView? - iphone

The documentation doesn't talk much about it, and there seems to be no init method for this? How would I create one and set the longitude and latitude or region to display in the map view?

First, add MapKit.framework.
Then, in .h file
#import <MapKit/MapKit.h>
and add delegate <MKMapViewDelegate>.
Then, in .m File, add the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:mapView];
}

You can include MKMapView both by code or by Interface builder.
For Interface builder just drag it & drop it to your xib.(Tools->Library->MapView)
By code
In your .h file
MKMapView * mapView;
In your .m file
-(void)viewWillAppear:(BOOL)animated
{
self.mapView = [[[MKMapView alloc] initWithFrame:self.view.frame] autorelease];
[self.view addSubview:self.mapView];
}

Interface builder includes the MKMapView (Map View). Drag the element into your XIB, add a referencing outlet in your controller, link them up. Then, set the region. Lots of good examples:
http://developer.apple.com/iphone/library/samplecode/WorldCities/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009466

mapview sample coding to find an location
#interface mapViewController ()
#end
#implementation mapViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title=self.name;
CLLocationCoordinate2D myCoordinate =
_mapView.userLocation.coordinate;
myCoordinate.latitude =[self.lat doubleValue];
myCoordinate.longitude =[self.lng doubleValue];
// NSLog(#"--->%#",self.lat);
// NSLog(#"--->%#",self.lng);
//set location and zoom level
MKCoordinateRegion viewRegion =
MKCoordinateRegionMakeWithDistance(myCoordinate, 1000, 1000);
MKCoordinateRegion adjustedRegion = [self.mapView
regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
// Set your annotation to point at your coordinate
point.coordinate = myCoordinate;
point.title = self.address;
//Drop pin on map
[self.mapView addAnnotation:point];
self.mapView.delegate = self;
// Do any additional setup after loading the view.
}

(void)viewDidLoad {
[super viewDidLoad];
MKMapView *myMapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myMapView];
}

Related

Display Google Map in Specific Place

I'm new to IOS development and I'm trying to add Google Maps to a sample App. I'm using the tutorial at https://developers.google.com/maps/documentation/ios/. Everything is working fine except that the Google Map is taking the entire screen. The code for displaying the Google Map is
#import <GoogleMaps/GoogleMaps.h>
#import "DemoViewController.h"
#implementation DemoViewController
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
zoom:6];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.animated = YES;
self.view = mapView;
}
#end
The reason why the Google Map is taking the entire screen is because of self.view = mapView;. How can I add the Google Map so that it doesn't take the up the full screen.
I tried using a SubView as follow but it is still not working. THe codes are:
ViewController.h
#interface ViewController : UIViewController
#property UIView *myView;
#end
ViewController.m
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController ()
#end
#implementation ViewController{
GMSMapView *mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect viewRect = CGRectMake(0, 0, 100, 100);
_myView = [[UIView alloc] initWithFrame:viewRect];
[self.view addSubview:_myView];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Try to replace self.view = mapView_; in your viewDidLoad method with:
[self.view addSubview:mapView_];
This adds your mapview to the current view as a subview.
EDIT
Try setting the frame of you mapView. Before [self.view addSubview:mapView_]; try:
mapView_.frame = CGRectMake(10,10,100,100)];
Change 10,10,100,100 for the frame you want for your mapview.

Adding a subview like UISlider or UIButtom in Google Maps in iPhone

I am new to iOS Programming and have very little knowledge about COCOA Touch. I am trying to add a button on the screen at the bottom over Google maps for giving an option to the user to go back to the previous screen. I just know that UIButton is a subclass of UIView and we can make button appear on a view by making it the sub view of that class. Previously iOS used to use Google Maps by default by in MKMapView and I have seen examples in books an on the Internet showing screen shots of apps where a button or a text box would appear on the map. But now just dragging the button in the interface builder doesn't help.
Here is my code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *btn;
#end
ViewController.m
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController ()
#end
#implementation ViewController
{
GMSMapView *mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadView
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
//Latitude and longitude of the current location of the device.
double lati = locationManager.location.coordinate.latitude;
double longi = locationManager.location.coordinate.longitude;
NSLog(#"Latitude = %f", lati);
NSLog(#"Longitude = %f", longi);
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
// Create a GMSCameraPosition that tells the map to display the coordinate
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lati
longitude:longi
zoom:11.5];
mapView_ = [GMSMapView mapWithFrame:[[UIScreen mainScreen] bounds] camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lati, longi);
marker.title = #"It's Me";
marker.snippet = #"My Location";
marker.map = mapView_;
[mapView_ addSubview:_btn];
[mapView_ bringSubviewToFront:_btn];
}
#end
Please let me know how it can be done.
Thanks.
Do normal UI building on your current view, then add the GMSMapView as subview (at index 0) of the self.view (DON'T do self.view = mapView;)
Here is the significant code:
mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view insertSubview:mapView atIndex:0];
Inserting the map view at index 0 will set the rest of the objects in front.
i suggest creating your button programmatically not using storyBoard, i tested this on "Google Maps SDK version: 1.4.0.4450" and it worked.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10, 10, 100, 40);
[button setTitle:#"title" forState:UIControlStateNormal];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.279526
longitude:-96.987305
zoom:2];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];;
self.view = self.mapView;
[self.view addSubview:button];

how should i set my current location in maps as my start point?

I'm a beginner for ios development. What I need is im set my start point as my current location?
- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 372.0f)];
self.view = contentView;
routeMapView = [[MKMapView alloc] initWithFrame:contentView.frame];
routeMapView.delegate = self;
routeMapView.showsUserLocation = YES;
[contentView addSubview:routeMapView];
routeOverlayView = [[UICRouteOverlayMapView alloc] initWithMapView:routeMapView];
diretions = [UICGDirections sharedDirections];
diretions.delegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D sOri,sDest;
startPoint=#"";
endPoint= #"34 Jalan Yew, 55100 Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur, Malaysia";
}
You can find the doc of MKMapView here.
Manipulating the Visible Portion of the Map part is what you're looking for.

Show another View Controller from an annotation callout button in MKMapKit

I am making an app that will use the MKMapKit for the pins and annotations. I want to display more information besides some text, so I created a button in the callout so I could make that go to another view controller. At the moment, the button in the callout doesn't do anything, as there is no code to make it work. It would be great if someone could make the code for the button to go to a different view controller!
The header file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#define METERS_PER_MILE 1609.344
#interface MSKSecondViewController :UIViewController<MKMapViewDelegate>
{
IBOutlet MKMapView *stillwellMapView;
int difficultyOfTrailOverlay;
}
#property (nonatomic, assign) CLLocationCoordinate2D mainEntranceToStillwellCoordinate;
#end
Here is the implementation file
#import "MSKSecondViewController.h"
#interface MSKSecondViewController ()
#end
#implementation MSKSecondViewController
#synthesize mainEntranceToStillwellCoordinate;
- (void)viewDidLoad
{
[super viewDidLoad];
stillwellMapView.delegate=self;
// Do any additional setup after loading the view, typically from a nib.
[self mainEntrancetoStillwellCoordinate];
[self bambooForestCoordinate];
}
- (void)mainEntrancetoStillwellCoordinate
{
MKPointAnnotation * main = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D mainLocation;
mainLocation.latitude = 40.831685;
mainLocation.longitude = -73.477453;
[main setCoordinate:mainLocation];
[main setTitle:#"Entrance"];
[main setSubtitle:#"Main"];
[stillwellMapView addAnnotation:main];
}
- (void)bambooForestCoordinate
{
MKPointAnnotation * bambooForest = [[MKPointAnnotation alloc]init];
CLLocationCoordinate2D bambooForestLocation;
bambooForestLocation.latitude = 40.829118;
bambooForestLocation.longitude = -73.466443;
[bambooForest setCoordinate:bambooForestLocation];
[bambooForest setTitle:#"Bamboo Forest"];
[bambooForest setSubtitle:#"Exit to Woodbury"];
[stillwellMapView addAnnotation:bambooForest];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation
(id<MKAnnotation>)annotation
{
MKPinAnnotationView * annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:nil];
UIButton *moreInformationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[moreInformationButton addTarget:self action:#selector(moreInformationButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = moreInformationButton;
moreInformationButton.frame = CGRectMake(0, 0, 23, 23);
moreInformationButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
moreInformationButton.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter;
annView.pinColor =MKPinAnnotationColorGreen;
annView.animatesDrop = true;
annView.canShowCallout = TRUE;
return annView;
}
I'm not sure if this is what I need to add, and what needs to go inside here
-(void) moreInformationButtonPressed:(UIButton *)sender
{
}
Write below kind of code:
-(void) moreInformationButtonPressed:(UIButton *)sender
{
OtherViewController *controller = [[OtherViewController alloc]initWithNibName:#"OtherViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
first you should make a tag that you can know which annotation is clicked
moreInformationButton.tag = someIndex;
then do the action you want
-(void) moreInformationButtonPressed:(UIButton *)sender
{
int index = sender.tag;
YourController *yc =[[[YourController alloc] initWithIndex:index] autorelease];
[self.navigationController pushViewController:scanView animated:YES];
}

SimpleView Based app with id initWithFrame Not working

I have app to create a graph i have created a view based app and then add the code for creating graph in it but it does not disply the graph. If use same code to create a separate UIView then it works other wise not
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
#class GraphsViewController;
#interface Display : UIView {
NSArray *percentages;
int myY;
ECGraph *graph;
ECGraphItem *item1;
ECGraphItem *item2;
}
#property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
#end
#import "Display.h"
#import "ECGraph.h"
#implementation Display
#synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];
item1 = [[ECGraphItem alloc] init];
item2 = [[ECGraphItem alloc] init];
/*
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];*/
item1.isPercentage = YES;
item1.yValue=myY;
item1.width = 35;
item1.name = #"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35;
item2.name = #"item2";
[graph setXaxisTitle:#"name"];
[graph setYaxisTitle:#"Percentage"];
[graph setGraphicTitle:#"Histogram"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
I am adding this view in GraphsViewController but its not showing anything
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self createGraph];
percentages = [NSArray arrayWithObjects:#"80",#"17", nil];
display = [[Display alloc] init];
[self.view addSubview:display];
[display setPercentageArray:percentages];
}
You should do the drawing in a separate UIView object and add it as a subview to your view controller's view. That's the way it is supposed to work.
The UIView class uses an on-demand drawing model for presenting
content.
Source: View Programming Guide for iOS
as opposed to
The UIViewController class provides the fundamental view-management
model for all iOS apps. ... A view controller manages a set of views
that make up a portion of your app’s user interface.
Source: UIViewController Class Reference
Edit:
// ...
display = [[Display alloc] init];
CGRect dFrame = CGRectMake(50, 50, 320, 200); // change these to whatever values you need
[display setFrame:dFrame];
[self.view addSubview:display];