I just have a question and can't seem to find it anywhere.
I"m new to iOS development and trying to use Google Maps inside my application.
I went thru the example they give you here.
#import "DemoViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#implementation DemoViewController {
GMSMapView *mapView_;
}
- (void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
longitude:103.848
zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = mapView_;
}
#end
But as you can see the they set self.view = mapView_; and the UIView class doesn't have a view function.
I want the map to be inside a UIView I have that is inside another ViewController
Did I lose you yet? Either ways here is a picture.
So inside of the view (or whitespace) I want the map to load.
Thanks guys for the help.
So you've got a GMSMapView. And you can make one view a subview of another in the interface with addSubview:. I wouldn't do it in loadView if I were you, though. viewDidLoad is the earliest good opportunity.
I think your real problem is that you're way ahead of yourself. You're trying to do this without know how views work, how view controllers work, etc. I recommend you take a deep breath and learn about iOS programming before you jump in with all four feet. Otherwise you don't know what your code (or Google's code) even means.
I found a solution that works for me if anyone needs to do something similar.
I just used a container view and then made separate UIViewController class (that has the google maps sdk code add a map section) and then hooked it up to my main ViewController by using [self.view addSubview:googleMapsView]; googleMapsView being my container view that I connected to the main ViewController.
I did an exact same thing here.
Simply added a View of type GMSMapView via interface builder and then set up the properties in controller.
Please try below code.
#property (strong, nonatomic) IBOutlet GMSMapView *mapView;
-(void)addAllPinsOnMapView
{
arrMapPin=[[NSMutableArray alloc] init];
for(int i = 0; i < arrOfferList.count; i++)
{
GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:[[[arrOfferList objectAtIndex:i] objectForKey:#"latitude"] floatValue] longitude:[[[arrOfferList objectAtIndex:i] objectForKey:#"longitude"] floatValue] zoom:12];
// mapView = [GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
_mapView.camera = cameraPosition;
_mapView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake([[[arrOfferList objectAtIndex:i] objectForKey:#"latitude"] floatValue], [[[arrOfferList objectAtIndex:i] objectForKey:#"longitude"] floatValue]);
marker.title = [[arrOfferList objectAtIndex:i] objectForKey:#"business"];
marker.map=_mapView;
_mapView.delegate = self;
}
}
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:
(GMSMarker *)marker {
NSPredicate *result;
result=[NSPredicate predicateWithFormat:#"business CONTAINS[c] %#",marker.title];
NSArray * array = [arrOfferList filteredArrayUsingPredicate:result];
OfferDetailsViewController *objOfferDetailsViewController = [[OfferDetailsViewController alloc]init];
objOfferDetailsViewController.dictOfferDetails=[array objectAtIndex:0];
[self.navigationController pushViewController:objOfferDetailsViewController animated:YES];
}
- (IBAction)btnLocateMe:(UIButton *)sender
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[[[AppDelegate initAppDelegate].DictLocation objectForKey:#"Latitude"] floatValue] longitude:[[[AppDelegate initAppDelegate].DictLocation objectForKey:#"Longitude"] floatValue] zoom:14];
[_mapView animateToCameraPosition:camera];
}
Related
I am trying to integrate a Google map in my iOS application using the Google map API. However, the map doesn't want to load. I am pretty sure that I integrated the right API key, and the appropriate framework. What is wired is that I don't have the map loaded, but have the Google Logo at the bottom of the page. Here is the code of the viewController Page, and a snapshot of the execution:
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#implementation ViewController{
GMSMapView *mapView_;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.53805 longitude:-5.0766 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
GMSMarker *marker = [[GMSMarker alloc]init];
marker.title = #"Lab 7";
marker.snippet = #"Al Akhawayn University in Ifrane";
marker.map = mapView_;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The first thing I'm seeing wrong here is the way you've created your marker. You need to inform the coordinates of it, something like this:
CLLocationCoordinate2D addressLocation = CLLocationCoordinate2DMake(self.initialLat, self.initialLong);
GMSMarker *marker = [GMSMarker markerWithPosition: addressLocation];
marker.title = #"Lab 7";
marker.snippet = #"Al Akhawayn University in Ifrane";
marker.map = mapView_;
Second thing, when you write:
mapView_.myLocationEnabled = YES;
You're telling the mapView to focus in your location, ignoring your camera coordinates you set earlier.
What might be causing your beige screen with Google's logo is that the Simulator doesn't have a built-in GPS, so your localization won't show on the simulator. Try commenting that line...
That was all I could think of with this code you've posted.
I am trying to implement my current location within a sub view using Google Map SDK for IOS. I am trying to implement the GMSMapView as an IBOutlet.
My header file has the code
#import <UIKit/UIKit.h>
#import <Google-Maps-iOS-SDK/GoogleMaps/GoogleMaps.h>
#interface TripLandingViewController : UIViewController
#property IBOutlet GMSMapView *mapView;
#end
My .m file has the code
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Tile.png"]];
self.mapView.myLocationEnabled = TRUE;
NSLog(#"\n\n\n My Location is %f,%f \n\n\n",self.mapView.myLocation.coordinate.latitude,self.mapView.myLocation.coordinate.longitude);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.mapView.myLocation.coordinate.latitude
longitude:self.mapView.myLocation.coordinate.longitude
zoom:10];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
No matter, what hardcoded value i provide as parameters for coordinates in camera. I am getting the wrong location (here UK) which is not at all updating.
But Instead when i tried
self.View = [GMSMapView mapWithFrame:CGRectZero camera:camera];
I was able to load correct map View in the Main View with correct location coordinates.
I tried all the answers on adding Google Map to SubViews through stackoverflow.
Cannot put a google maps GMSMapView in a subview of main main view?
How to set the frame for mapview - in google GMSMap for iOS6
How put Google MapView in UIView?
Google Maps iOS SDK, Getting Current Location of user
But nothing seems to work. Please Help since I am new to IOS Programming.
Use:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:0 longitude:0 zoom:15];
self.mapView.camera = camera;
instead of using:
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
I'm not totally sure why, but this method works when your GMSMapView is a subview of your UIViewController.
I use google maps iOS SDK with storyboard. Application starting with Navigation View Controller that has a Root View Controller with map.
#implementation MyViewController
#synthesize btnMyLock;
#synthesize btnNearby;
GMSMapView *mapView_;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:20
longitude:20
zoom:0];
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 setIcon:[UIImage imageNamed:#"pin"]];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
mapView_.settings.compassButton = YES;
[mapView_ addSubview:btnMyLock];
[mapView_ addSubview:btnNearby];
}
Button btnMyLock push the Table View Controller. In iOS 7 it's ok. But iOS 6 my app crashes. Sometimes crash with EXC_BAD_ACCESS (code=1) or code=2
Problem that I used Autolayout view.
you forgot step 4: "Drag the GoogleMaps.bundle" from the Resources folder to your project.
I suggest putting it in the Frameworks group. When prompted, ensure "Copy items into destination group’s folder" is not selected. i encountered the same problem, and this fixed it.
I don't know if it's causing the problem, but I wouldn't do:
self.view = mapView_;
Just add the mapView_ as a subview, like:
[self.view addSubview:mapView_];
In general EXC_BAD_ACCESS crashes are caused when you are mismanaging memory (e.g. an object is being deallocated prematurely). Try to find out what object the crash is happening on.
Create your buttons in code, not from the xib as IBOutlets. Otherwise they are not loaded since you are not using the xib.
Use this code when you use Google map and then check its not crash when you navigate to another screen.
- (void)dealloc {
[super dealloc];
[mapView_ removeObserver:self
forKeyPath:#"myLocation"
context:NULL];
}
Apple has taken out google map from ios 6.0 version onwards.Apple Using MkMapView that was not that much clear like google map..I used GoogleMapOverlay it displayed the Google map inside the MkMapView..GoogleMapOverlay displays the worldMap i want to move to specific loaction using latitude and longitude...If i specify the latitude and longitude position in GoogleMapOverlay example am getting that particular position alone in rectangle shape overlay at the back i can see my mkmapview...can any one please guide me to resolve this..Thank you
You can use Google Maps on iOS 6 by adding a UIWebView and adding Map on it.
You can use the google map sdk for >= ios6
https://developers.google.com/maps/documentation/ios/
here is sample code from google sdk ios
#import "YourViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#implementation YourViewController {
GMSMapView *mapView_;
}
// You don't need to modify the default initWithNibName:bundle: method.
- (void)loadView {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:24.451794 longitude:54.391412 zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position=CLLocationCoordinate2DMake(24.451794,54.391412);
marker.snippet = #"Abu Dhabi";
marker.title = #"Al Jazira Sports and Cultural Club";
marker.map = mapView_;
}
#end
output:
as the title says no matter what coordinates I give I see the same location.
Here is my code:
I am using storyboard and I have a subview inside my view. The subview is of type GMSMapView, and correctly linked.
.h
#property (strong, nonatomic) IBOutlet GMSMapView *mymap;
.m
#implementation DealViewController
{
GMSMapView *mapView_;
}
#synthesize mymap=_mymap;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view layoutIfNeeded];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.mapType = kGMSTypeSatellite;
mapView_.delegate = self;
self.mymap=mapView_;
}
I also tried this, but I get the same:
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
What is wrong with the above?
After hours of cruel research of the same problem I've found that viewDidLoad method should looks like this:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:obj.latitude
longitude:obj.longitude
zoom:12];
self.mapView.camera = camera;
}
In other words in this case you should NOT assign any value (like '[GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];') to self.mapView, just set it's property 'camera' value.
This works for me. I hope this will save time for someone else too.
If you are using storyboards make sure you have set the type of the view to be a GMSMapView in the storyboard UI.
Then all you should need to do is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view layoutIfNeeded];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
self.mymap.camera = camera;
self.mymap.myLocationEnabled = YES;
self.mymap.mapType = kGMSTypeSatellite;
self.mymap.delegate = self;
}
There is a storyboard example that is available on the Google Maps github page.
I think this is your problem:
#property (strong, nonatomic) IBOutlet GMSMapView *mymap;
#implementation DealViewController
{
GMSMapView *mapView_;
}
#synthesize mymap=_mymap;
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];
Your map is coming in from your storyboard, called mymap. You create an instance variable called mapView_ but you synthesize mymap to _mymap...which is different.
Rather than using an instance variable, just access the property directly. Replace all the mapView_ instances with self.mymap.
For example:
self.mymap = [GMSMapView mapWithFrame:CGRectMake(0, 0, 300, 300) camera:camera];
As to why it doesn't move to the user's location - here's what the Google Maps iOS SDK documentation has to say about the myLocationEnabled property:
myLocationEnabled - Controls whether the My Location dot and accuracy circle is enabled.
...what it doesn't do is move the map to the current user's location. It only shows the user's location dot and circle on the map.
Your map is always displaying the same location because you never change it. You create a camera set to (-33.8683, 151.2086), ask the map to show the user's location indicator, but don't ask the map to change its location to that of the user.
To do this you can use the map's myLocation property to obtain the current location of the user, and then the animateToLocation method to move the map accordingly.
GMSMapView seems to struggle fitting to GMSCoordinateBounds in these situations:
1) You init the GMSMapView with a .zero frame
2) The GMSMapView is not yet visible on the screen
3) The GMSMapView is a subview of any given UIViewController
In any of these three cases you have to set the camera explicitly:
let camera = GMSCameraPosition.camera(withTarget: B.center, zoom: 19)
self.map.camera = camera