Google map load on subviews in IOS - ios

I have implement google map in IOS . But the problem is I can not implement google map on my custom view (subview) .
Thanks in advance .

Create a subview and Define the subview class under GMSMapView
- (void)LoadMap {
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_;
// this line add subView
[self.view addSubview : mapView_ ]; }

Related

iOS Objective-C Google map shows only on debug when added to a subview

I'm using google maps SDK, first all was working fine but it was shown as a full screen. After adding the map into a subview, the map is no more shown. I added a breakpoint on the IBAction and noticed that after passing:
self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];
the map is shown perfectly in the subview.
Thats my code in IBAction:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];
self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];
self.mapView1.myLocationEnabled = YES;
[_mapView1 setMinZoom:12 maxZoom:20];
self.mapView1.mapType = kGMSTypeSatellite;
self.mapView1.settings.compassButton = YES;
_mapView1.delegate = self;
[_mapView1 animateToViewingAngle:45];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = #"City";
marker.snippet = #"Country";
marker.map = _mapView1;
[_subview addSubview:_mapView1];
thats how i want it to look like
Thanks in advance.
Try this it's working for me.
GMSMapView *customView = [[GMSMapView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];
customView = [GMSMapView mapWithFrame:customView.bounds camera:camera];
customView.myLocationEnabled = YES;
[customView setMinZoom:12 maxZoom:20];
customView.mapType = kGMSTypeSatellite;
customView.delegate = self;
[customView animateToViewingAngle:45];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = #"City";
marker.snippet = #"Country";
marker.map = customView;
[self.view addSubview:customView];
OR
GMSMapView *customView = [[GMSMapView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:33.67 longitude:35.55 zoom:12];
_mapView = [GMSMapView mapWithFrame:customView.bounds camera:camera];
_mapView.myLocationEnabled = YES;
[_mapView setMinZoom:12 maxZoom:20];
_mapView.mapType = kGMSTypeSatellite;
_mapView.settings.compassButton = YES;
_mapView.delegate = self;
[_mapView animateToViewingAngle:45];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(33.67, 35.55);
marker.title = #"City";
marker.snippet = #"Country";
marker.map = _mapView;
[customView addSubview:_mapView];
[self.view addSubview:customView];
Also check your _subview hidden or not.
Since i can't comment, i will use this way to ask you to do some stuff:
1- Add a breankpoint to this line and check _subview.bounds values.
self.mapView1 = [GMSMapView mapWithFrame:_subview.bounds camera:camera];
2- While running in the simulator click on Debug > View Debugging > Capture View Hierarchy and locate your AVPlayer, check the properties on the right panel.
Check the _subview.bounds when you are creating the GMSMapView.
Depending on the time this is done, it might not be setup and you can end-up with a CGRectZero frame.

How to add a marker in `Google Maps` view in Objective-C(iOS)

I am trying to add markers to Google Maps from data in my Web Service. Now how to show Marker that only in "specific radius distance". I'm trying but didn't have a logic on how to implement that in Objective-C.
It works for me.Please try it.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:(sourceValue.latValue + destinationValue.latValue)/2
longitude:(sourceValue.lngValue + destinationValue.lngValue)/2
zoom:7.5];
mapview = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[self.view addSubview:mapview];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(sourceValue.latValue, sourceValue.lngValue);
marker.map = mapview;
//Place source mark of destination
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(destinationValue.latValue, destinationValue.lngValue);
marker2.map = mapview;
Change your lat, lang value according to your requirements.Thanks

app is crashing without any error

I am trying to integrate the google map sdk and followed this link to start from google. I have created the API key and used it in appDelegate. I wrote below code in my ViewController.m and I have imported all the frameworks required, and also put the -ObjC in other linker flag but still the app is crashing on run time. I have used breakpoints but no log is been shown up. Below is the entire code and haven't used any outlet.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // crashing here, even gave different frames rather CGRectZero
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_;
}
#end
DEMO SAMPLE LINK
I downloaded the code and run it. It works perfectly well. Just remove the old app and clear the target. Re-install the app. It should work fine.
Why do you pass CGRectZero to the frame of the map?
Try the following code:-
- (void)viewDidLoad {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20 zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 320) camera:camera]; // crashing here
mapView_.myLocationEnabled = YES;
[self.view addSubview: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_;
}

How to reduce the screen size of google map in ios application

In my application I have included google maps API but the problem is, it is occupying the entire screen but I dont want it occupy entire screen as I have other UI controls on the same screen. I have my code pasted below.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:12.9699 longitude:77.6499 zoom:6];
mapView_ = [GMSMapView mapWithFrame: CGRectMake(0,0, 10, 25) camera: camera];
mapView_.myLocationEnabled = YES;
NSLog(#"User's location: %#", mapView_.myLocation);
UIEdgeInsets mapInsets = UIEdgeInsetsMake(100.0,10.0, 10.0, 300.0);
mapView_.padding = mapInsets;
self.view = mapView_;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(12.9699,77.6499);
marker.title = #"BANGALORE";
marker.snippet = #"IndraNagar";
marker.map = mapView_;
self.view = mapView_; instead of this add below code
UIView * MApBaseView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];// add your frame size here
[self.view addSubview:MApBaseView];
[MApBaseView addSubview: mapView_];
Try with this code, it work for me:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:14];
mapView = [GMSMapView mapWithFrame:mapViewContent.frame camera:camera];
mapView.myLocationEnabled = YES;
mapView.delegate = self;
[mapViewContent addSubview:mapView];
mapViewContent is your view custom.
You should replace marker.map = mapView_; as [self.view addSubview:mapView_];
See the below code
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
longitude:locationManager.location.coordinate.longitude
zoom:16];
mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 44, self.view.bounds.size.width, self.view.bounds.size.height-44) camera:camera];
mapView_.myLocationEnabled = YES;
[self.view addSubview:mapView_];

GMSMarker points not being shown

I have a viewcontroller in a storyboard with a View in it that loads googlemaps. Markers are however, not showing up!
What have I done wrong?
Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createBasicGSMView];
}
- (IBAction)AddPointButtonPressed:(id)sender {
// Creates a marker in the center of the map.
GMSMarker *CurrentLocationMarker = [[GMSMarker alloc] init];
CLLocation *CurrentLocationNote = mapView_.myLocation;
CurrentLocationMarker.position = CLLocationCoordinate2DMake(CurrentLocationNote.coordinate.latitude, CurrentLocationNote.coordinate.longitude);
CurrentLocationMarker.title = #"Current Location";
CurrentLocationMarker.snippet = #"This is the place to be";
[CurrentLocationMarker setMap:mapView_];
}
- (void) createBasicGSMView{
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:1];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.mapViewHolder = 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_;
}
Figured it out. had to add the view as a subview:
[self.view addSubview:mapView_];
Doesnt work otherwise!

Resources