There's something like CameraUpdateFactory to iOS? - ios

I want to update my camera fitting it to bounds and scrolling it at the same action, but I couldn't achieve it.
If I try to do both at the same time but different animation I get myself in a strange place of the map (iPhone).
The code above works in Simulator, but not in iPhone.
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:_restCoord coordinate:_houseCoord];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:190.0f];
[_mapView_ moveCamera:update];
GMSCameraUpdate *downwards = [GMSCameraUpdate scrollByX:0 Y:-90];
[_mapView_ moveCamera:downwards];
I saw "CameraUpdateFactory" to Android that could achieve my needs, there's something like that to iOS? I didn't find.
Thanks in advance.

It looks like there is an issue somewhere between a padding value of 150 and 160 on an iPhone. My suspicion is that 160 * 2 = 320, which is approximately the point width of an iPhone, and thus we are asking the map for the bounded area to take up negative space in the map.
This code works:
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:_restCoord coordinate:_houseCoord];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:150.0f];
[mapView moveCamera:update];
GMSCameraUpdate *downwards = [GMSCameraUpdate scrollByX:0 Y:-90];
[mapView moveCamera:downwards];

Related

Google Maps iOS Location is centered in corner of MapView

I am trying to load a Google Map into my application within a subview. When I initialize my GMSMapView in iOS, the current location is in the top left corner of the map instead of the center screen. This is true if I were to press the "My location" button as well.
This seems to be an issue when running on my phones but not on simulators. How do I get it so that it is set up correctly in the center?
I've tried rearranging code and loading the map differently. I think it might be due to Autolayout Constraints, as when I set mapView.translatesAutoresizingMaskIntoConstraints = true; then the location is in the center, but my view is messed up.
I currently have GMSMapView set up as the custom class of my view with auto layout constraints set up to resize it.
I load up my GPS class and set the frame with this.
gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.mainView.frame), CGRectGetHeight(self.mainView.frame)-CGRectGetHeight(self.topView.frame)-CGRectGetMaxY(self.segmentTopView.frame));
[self.segmentView addSubview:gpsVC.view];
And in my GPSViewController I set up the map camera as follows:
self.mapView.myLocationEnabled = YES;
GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude longitude:myLocation.coordinate.longitude zoom:[mapDefaultZoomLevel floatValue]];
self.mapView.camera=camera;
Is anyone else experiencing this, and did you find any solution to this problem?
Edit: Here are some images (Links because I can't put in images yet)
https://imgur.com/FEMfwri
https://imgur.com/ySQio5b
https://imgur.com/9kijxbT
for anyone wondering I eventually found a workaround solution to the problem I was having.
I believe that there was a view frame issue with auto-layout that was happening when my GPSViewController initialized. I avoided this issue by setting up my Google Map after my view has been setup. I removed the GMSMapView from the custom class of my UIView and instead programmatically created the map manually and added it to the view.
In my MasterViewController.m:
gpsVC = [[GPSViewController alloc] init];
gpsVC.view.frame=CGRectMake(0, CGRectGetMaxY(self.segmentTopView.frame), CGRectGetWidth(self.segmentView.frame), CGRectGetHeight(self.segmentView.frame)-CGRectGetHeight(self.segmentTopView.frame));
[gpsVC setupMap];
In my GPSViewController.m (abridged):
-(void)setupMap{
GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:coordinate.latitude longitude:coordinate.longitude zoom:mapDefaultZoomLevel];
self.mapView = [GMSMapView mapWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.settings.myLocationButton = YES;
self.mapView.settings.compassButton = YES;
self.mapView.delegate=self;
[self.myView addSubview:self.mapView];
}
Looks like you are doing it correctly, the only thing I can't say for sure is whether you are centering your map correctly in the sub view. But here are some other map tricks you can try, if the problem is with the map:
[self.mapView clear];
// Set map view and display
double zoom = 14;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[myLocation.latitude doubleValue]
longitude:[myLocation.longitude doubleValue]
zoom:zoom];
// Initialize Map View
self.mapView.camera = camera;
self.mapView.myLocationEnabled = YES;
__block GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
//You can try setting a couple of points east and west of your current location and including them in the bounds
CLLocation westLocation = myLocation;
westLocation.longitude += .005;
CLLocation eastLocation = myLocation;
eastLocation.longitude -= .005;
bounds = [bounds includingCoordinate:myLocation.position];
bounds = [bounds includingCoordinate:eastLocation.position];
bounds = [bounds includingCoordinate:westLocation.position];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:40.0f]];
} else {
[self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];
}
Same issue with you. I was delay init google mapview to test. And It works normally again.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self initGoogleMap];
});

Positioning camera that shows two places in GMSMapview iOS

I am using Google Map service GMSMapview in my iOS Application. In that I have two CLLocationCoordinates. One is Current Location and other is Destination Location. I am trying to fit the map within those two places.
But the camera position is not set within those two points. I tried with the following code,
CLLocationCoordinate2D start = CLLocationCoordinate2DMake(newLoc.coordinate.latitude, newLoc.coordinate.longitude);
CLLocationCoordinate2D end = CLLocationCoordinate2DMake(_retailerVO.R_Latitude, _retailerVO.R_Longitude);
GMSCoordinateBounds *gBounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];
GMSCameraPosition *gCamera = [mapView_ cameraForBounds:gBounds insets:UIEdgeInsetsZero];
mapView_.camera = gCamera;
Is there any way to achieve what I am looking for? All suggestions are appreciated.
In Swift
let bounds = GMSCoordinateBounds(coordinate: sourcePosition, coordinate: endPosition)
let camera: GMSCameraUpdate = GMSCameraUpdate.fit(bounds)
// let cameraWithPadding: GMSCameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 100.0) (will put inset the bounding box from the view's edge)
self.mapView.animate(with: camera)
Add padding with bounds, it works for me, hope it helps you,
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:start coordinate:end];
[self.viewMapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:100.0f]];

iOS Google Map SDK : Change Google Map Zoom Level with GMSCameraUpdate:fitBounds:withEdgeInsets

I want to change iOS Google Map SDK zoom level using with GMSCameraUpdate:fitBounds:withEdgeInsets. But the problem is the Google map is taking the default zoom level when i use GMSCameraUpdate:fitBounds:withEdgeInsets. Here am using withEdgeInsets to change map position. Any help will be appreciated.
Below is my complete code.
GMSCoordinateBounds *markerBounds = [[GMSCoordinateBounds alloc] initWithPath:gmsPath];
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate fitBounds:markerBounds withEdgeInsets:UIEdgeInsetsMake(-500, 0, 0, 0)];
[mapView_ animateWithCameraUpdate:updatedCamera];
Here gmsPath contains multiple coordinates for [GMSMutablePath path].
When i use [mapView_ animateToZoom:10]; then withEdgeInsets is not working.
I searched google and stackoverflow but i could not find correct result.
You should probably resize the map to fit in the available space, rather than use an edge inset to have the same effect. In particular, resizing the map would make sure that the Google logo is visible, which is a requirement of using the SDK.
If you don't want to do that, then I think you should use something like UIEdgeInsetsMake(0, 0, 500, 0), ie a positive inset on the bottom edge, not a negative inset on the top edge. Instead of hard-coding 500, you should use the height of your table (in points).
I found the solution with UIEdgeInsetsMake. The following is the solution.
GMSCoordinateBounds *markerBounds = [[GMSCoordinateBounds alloc] initWithPath:gmsPath];
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate fitBounds:markerBounds withEdgeInsets:UIEdgeInsetsMake(44, 0, self.view.frame.size.height - 180, 0)];
[mapView_ animateWithCameraUpdate:updatedCamera];
-180 is the available space above UITableView

GMSMapView display whole world map?

I am adding my map view like
mapView_ = [[GMSMapView alloc]initWithFrame:_viewMapContainer.bounds];
mapView_.myLocationEnabled = YES;
mapView_.frame = _viewMapContainer.bounds;
[_viewMapContainer addSubview: mapView_];
In my application this view is only for displaying the whole map of the world and some markers. I am unable to display the whole world in one view.Any help will be appreciable ,I don't want the user interactions on it
I have also faced this problem
- (void)focusMapToShowAllMarkers
{
CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position;
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];
for (GMSMarker *marker in _markers)
bounds = [bounds includingCoordinate:marker.position];
[_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]];
}
Please try this and share the response.
happy coding :)

Map view not focus on annotation

Hello i am new to ios and showing an annoatation on map view using lat and long coming from server.
I have one view controller in which i am adding map and showing position of lat and long. But my problem is whenever i am adding annotation map is not focusing on annotattion insted of it everytime i want pinch map view then map is going to at annoatation i dont know why this is happening?
zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];
[mapView addAnnotation:annotationPoint];
here is my code adding in viewwillappera
You need to center the map by use the following code.
mapView.centerCoordinate = annotationPoint.coordinate;
Milan's solution does what you need without animation.
If you want to do this with animation, you should use:
[mapView setCenterCoordinate:location animated:YES];

Resources