Embed Google Maps in UIView with correct Frame - ios

In my iOS application, I have a parent mapViewController(UIView) to which I try to add to Google Maps subview by executing [self.mapViewContainer addSubview:mapView_];
While initialising the mapView_, I try to use the bounds of the container UIView, so that it utlisises the entire space by executing:
mapView_ = [GMSMapView mapWithFrame:self.mapViewContainer.bounds camera:camera];
While the map appears just fine, whenever I try to center the map on coordinate, the cordinate which is supposed to be centered is either at the bottom right, or bottom center, depending on potrait / landscape mode.
My impression is that the map extends beyond the intended margins, which I'm unable to fix.
Is there any way to easily accomplish what I'm trying to do. I have tried initialising mapView_ as
GMSMapView *mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
But that just displayed an empty screen.

You can place Google Map View in storyboard if possible.
If you want to do in code, avoid using frame, try use constraints.

following line google map not shown because you use CGRectZero.so that's why not display your google map.
GMSMapView *mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
This code work for me...you may be helped..
See my code...you show your google map in view controller.
GMSCameraPosition *camera ;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
camera = [GMSCameraPosition cameraWithLatitude:<pass your latitude>
longitude:<pass your longitude>
zoom:17];
}
else
{
camera = [GMSCameraPosition cameraWithLatitude:<pass your latitude>
longitude:<pass your longitude>
zoom:16];
}
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectMake(0,0, self.view.frame.size.width , self.view.frame.size.height) camera:camera];
mapView.myLocationEnabled = YES;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
[mapView setMinZoom:17 maxZoom:23];
}
else
{
[mapView setMinZoom:16 maxZoom:20];
}
// [mapView setMinZoom:16 maxZoom:20];
// mapView.settings.indoorPicker =YES;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(<pass your latitude>,<pass your longitude>);
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.title =#"Driver's location";
marker.icon = [UIImage imageNamed:#"GoogleIcon"];
marker.map = mapView;
[self.view addSubview:mapView];
[self.view insertSubview:self.btnRefresh aboveSubview:mapView];

Related

Not showing correct view after zooming on GMSMap

I have been using GMSMap in my app. I am showing current location in Map. But after zooming the Map it does not show city and state, also path is not visible in the map. I want to I have zooming on map and clear map in the present states and city's. Please assist me for my issue
I am used this code
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.6987 longitude:75.8817 zoom:0];
GMSMapView *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(22.6987, 75.8817);
marker.title = #"indore";
marker.snippet = #"India";
marker.map = mapView;
Now Show Look like please see ones

Blue dot for current location not displaying using google maps for iOS

I'm trying to implement current locations in my app. Please see my code for the viewDidLoad below:
- (void)viewDidLoad {
[super viewDidLoad];
[self hideTabBar:self.tabBarController];
// 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:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.accessibilityElementsHidden = NO;
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_;
}
I can see the map in the iOS simulator (iPhone 4s) with the marker set to Sydney, but no blue dot to be seen anywhere. Is current location perhaps working but the UI element is hidden?
Thanks for any help/suggestions!
In my app I need to implement the CLLocationManager and set NSLocationWhenInUseUsageDescriptionin the Custom Target Properties.
Hope this helps.
In simulator, you need to set custom location like following:
Debug > Locations > Custom Location... then enter a latitude and longitude for the location you would like to simulate.
NOTE: if this method didn't work, then just select Freeway Drive instead of Custom Location. Once you clicked on Freeway Drive, then do the same step as above mentioned.
func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
//if want to change the camera position
if self.latDouble != 0.0 && self.longDouble != 0.0
{
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: self.latDouble, longitude: self.longDouble, zoom: 18.0)
self.mapView.camera = camera
}
self.mapView.isMyLocationEnabled = true
self.mapView.reloadInputViews()
return true
}

Google Maps iOS MapView

I am trying to make my google maps map view only half of the screen. I have resized the map view to be half the screen and also changed my code so it's only the bounds of the map view which is half the screen but it still goes full screen. Anyone got a solution?
// setting up mapView
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
_mapView = [GMSMapView mapWithFrame:_mapView.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(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = _mapView;
// Creates a marker in the center of the map.
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(-36.86, 151.20);
marker2.title = #"Sydney2";
marker2.snippet = #"Australia2";
marker2.map = _mapView;
Thanks,
Curtis
You may can try to add the _mapView as a subview instead of assigning it to self.view.
[self.view addSubview:_mapView];
You can do it with the help of storyboard.
Drag a UIView in your ViewController and add GMSMapView class to the
UIView.
Set the frame size for your UIView using storyboard. Create outlet
for the UIView( name it mapView) and connect it.
In your ViewController class under viewDidLoad add these two lines
two view the google map.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: YOUR_LATITUDE
longitude: YOUR_LONGITUDE zoom:8];
[self.mapView setCamera:camera];
Add marker to the location using following code.
GMSMarker *marker = [[GMSMarker alloc]init];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(YOUR_LATITUDE, YOUR_LONGITUDE);

IOS google maps camera not point to the marker

I have a view controller in a story board and i want to show a google map on it
everything went good, i got the get and i can show the map
my problem is that the camera is not pointing to the marker
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"we are in the show stadium");
// 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:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.mynewmap = 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_;
}
where mynewmapis an UIView outlet
here is what I see
the marker is on austrail but the camera not pointing to it and the marker is not on the map
If you are working with Storyboards, set the class of mynewmap to GMSMapView in Interface Builder.
And in viewDidLoad just add the camera, you don't need to alloc the view because it will be already allocated:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
self.mynewmap.camera = camera;
}
Or try not creating your view with a Zero rect.. CGRectZero... you could try with:
mapView_ = [GMSMapView mapWithFrame:self.mynewmap.frame camera:camera];
Your current location & (-33.86,151.20) locations are same ya not ... You have enabled userlocation mode. Please try again after comment
//mapView_.myLocationEnabled = YES;
remove this line ... Because It's also tracking userlocation. May be help full.
Add this line after you are done with adding marker
[mapView_ animateToLocation:marker.position];
or use
[mapView_ animateToCameraPosition:[GMSCameraPosition cameraWithLatitude:marker.position.latitude
longitude:marker.position.longitude
zoom:6.0f]];
Faced the same issue in iOS 11, instead of setting the GMSMapView from the storyboards, initialized it in the viewDidLoad method of the viewcontroller like this:
override func viewDidLoad() {
super.viewDidLoad()
// Force the view to layout to get its proper frame
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
// Initialize the GMSMapView with target view's bounds
googleMapView = GMSMapView(frame: self.mapView.bounds)
// Add the GMSMapView to the map container view
self.mapView.addSubview(googleMapView)
}

Google Maps SDK for iOS issue when rotating to landscape

I have a Google Map from the Google Maps SDK for iOS (last version). I display a map like this in a UIScrollerView:
showMarker = YES;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[geocodeLatitude floatValue] longitude:[geocodeLongitude floatValue] zoom:13];
[self setupMapWithCamera:camera withLatitude:geocodeLatitude withLongitude:geocodeLongitude];
float mapHeight = 50;
[mapView_ setFrame:CGRectMake(0, 0, widthOfBlock, mapHeight)];
[self.scroller addSubview:mapView_];
The method called is:
-(void)setupMapWithCamera:(GMSCameraPosition *)camera withLatitude:(NSString *)Slatitude withLongitude:(NSString *)Slongitude {
// setup map
[mapView_ clear];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.settings.scrollGestures = NO;
mapView_.settings.zoomGestures = NO;
// setup marker
if (geocodesuccess || showMarker) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([Slatitude floatValue], [Slongitude floatValue]);
if ([ShopWithDatas.open isEqualToString:#"1"] || [ShopWithDatas.open2424 isEqualToString:#"1"]) {
marker.icon = [GMSMarker markerImageWithColor:[UIColor greenColor]];
} else {
marker.icon = [GMSMarker markerImageWithColor:[UIColor redColor]];
}
[mapView_ setSelectedMarker:marker];
marker.map = mapView_;
}
}
So, it works when you enter in this view from Portrait => Portrait.
It works when you enter in this view from Landscape => Landscape.
But the camera is not centered anymore when you go from Portrait => Portrait and then in the view change to Landscape. Plus, It works when you enter this view from Landscape => Landscape and then turn into Portrait.
Any idea how to fix the camera for Portrait => Landscape issue ?
Finally, even if I don't get why it does not update correctly, I found this solution:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if (canIshowMap) {
CLLocationCoordinate2D actualLocation = CLLocationCoordinate2DMake([geocodeLatitude floatValue], [geocodeLongitude floatValue]);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:actualLocation];
[mapView_ moveCamera:updatedCamera];
//[mapView_ animateToLocation:actualLocation];
}
}
Please note that the commented line does not fix the bug.

Resources