Google Maps SDK for iOS issue when rotating to landscape - ios

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.

Related

Embed Google Maps in UIView with correct Frame

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];

iOS Google Maps: Coordinates Mismatch

One of our application's feature is to plot a location on the map, we will save it in our server and show it on the main screen of the application. We are using Google Maps and GMSMarker with draggable property set to YES. But there is a difference between the marked location and the coordinate obtained.
Code:
NSDictionary *signDict = self.imgLocationsArray[_currentIndex];
_toSaveLat = [signDict[TAG_SIGN_LAT] floatValue];
_toSaveLon = [signDict[TAG_SIGN_LONG] floatValue];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:_toSaveLat longitude:_toSaveLon zoom:GOOGLE_MAPS_DEFAULT_ZOOM_LEVEL];
self.dragDropMap = [GMSMapView mapWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) camera:camera];
[self.dragDropMap setMapType:kGMSTypeNormal];
[self.dragDropMap setDelegate:self];
self.dragDropMap.myLocationEnabled = YES;
[self.view addSubview:self.dragDropMap];
if (draggingMarker) {
draggingMarker.map = nil;
draggingMarker = nil;
}
draggingMarker = [[GMSMarker alloc] init];
draggingMarker.icon = [GMSMarker markerImageWithColor:[UIColor plumColor]];//[UIImage imageNamed:#"MoveablePin"];
draggingMarker.position = CLLocationCoordinate2DMake(_toSaveLat, _toSaveLon);
draggingMarker.tappable = NO;
draggingMarker.draggable = YES;
draggingMarker.map = self.dragDropMap;
-(void)mapView:(GMSMapView *)mapView didEndDraggingMarker:(GMSMarker *)marker{
NSLog(#"marker dragged to location: %f,%f", marker.position.latitude, marker.position.longitude);
_toSaveLat = marker.position.latitude;
_toSaveLon = marker.position.longitude;
}
Am I missing something in the code or anything else? Please suggest a solution or workaround to get this fixed. Thanks!
Testing Environment
xCode - 7.0
Google Maps SDK version - 1.10.3 (Using pod)
Device - iPhone 5S, iPhone 6plus
iOS - 9.0.2

iOS Google Map stop scrolling beyond radius

How to stop scrolling beyond a radius. I need to restrict user to only scroll with in 20 meters of their current location.
I have tried below link:
Prevent scrolling outside map area on google map
But I am not able to find out the correct code to be put to implement current answer on this post.
You can use distanceFromLocation to calculate how far from your center coordinate point, then you can check if you scroll outside of the circle in the (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position delegate method.
Sample code:
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:11];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = 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;
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10000];
circ.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
circ.strokeColor = [UIColor redColor];
circ.strokeWidth = 5;
circ.map = mapView;
mapView.delegate = self;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.86, 151.20);
int radius = 10000;
CLLocation *targetLoc = [[CLLocation alloc] initWithLatitude:position.target.latitude longitude:position.target.longitude];
CLLocation *centerLoc = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
if ([targetLoc distanceFromLocation:centerLoc] > radius) {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:center.latitude
longitude:center.longitude
zoom:mapView.camera.zoom];
[mapView animateToCameraPosition: camera];
}
}

iOS Google map SDK: Setting maxZoom level issue

I notice when I set the max Zoom level of example to 19, the zoom go up to 20. I don't know why.
It's always 1 more zoom level than the one I set.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:23.589571946369546
longitude:58.14204730042655
zoom:16];
self.mapView_.camera=camera;
self.mapView_.myLocationEnabled = YES;
self.mapView_.mapType = kGMSTypeHybrid;
self.mapView_.settings.compassButton = YES;
[self.mapView_ setMinZoom:5 maxZoom:19];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(23.168520, 58.008163);
marker.map = self.mapView_;
// ------ add layer
// Implement GMSTileURLConstructor
// Returns a Tile based on the x,y,zoom coordinates, and the requested floor
GMSTileURLConstructor urls = ^(NSUInteger x, NSUInteger y, NSUInteger zoom) {
NSString *url = [NSString stringWithFormat:#"http://www.example.com/%tu/%tu/%tu.png", zoom, x, y];
NSLog(#"url=%#",url);
return [NSURL URLWithString:url];
};
How do you find out the maxZoom is 20? Which version of iOS Maps SDK you use?
I tried the sample hello map application from Google Maps' Github page with iOS Maps SDK version 1.9.1 and put the below code in the videDidLoad() method. It prints the correct max zoom which is 19.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:23.589571946369546
longitude:58.14204730042655
zoom:16];
mapView_.camera=camera;
mapView_.myLocationEnabled = YES;
mapView_.mapType = kGMSTypeHybrid;
mapView_.settings.compassButton = YES;
// Create the GMSMapView with the camera position.
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView_ setMinZoom:5 maxZoom:19];
NSLog(#"max zoom: %f", mapView_.maxZoom);

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
}

Resources