Positioning camera that shows two places in GMSMapview iOS - 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]];

Related

How to show a place-mark with specific angle information on Maps app (Apple Maps or Google Maps)?

I want to display Gunshot information on the Maps app. A gunshot has 2 main properties:
location - point from which the shot was fired
direction/angle - relative to Compass North
Using the following code snippet, I can open the Maps app with a specific place-mark.
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(gps_lat.doubleValue, gps_long.doubleValue);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:self.pUnit.unitLabel];
[mapItem openInMapsWithLaunchOptions:nil];
Is it possible to add angle/direction information along with the place mark? Basically the place-mark should rotate along with the map so as to clearly depict the direction of the gunshot.
you can achieve rotation of marker using google maps and rotation property of marker.
though you want obj-c code, my swift code will be helpful to understand you what I meant.
replace MARKER_IMAGE_NAME with your image
1) setup google map
let camera = GMSCameraPosition.camera(withLatitude: firstPoint.latitude,
longitude: firstPoint.longitude, zoom: 14)
let navHeight = (self.navigationController?.navigationBar.frame.size.height)! + 20
let frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height - navHeight)
let GMapView = GMSMapView.map(withFrame: frame, camera: camera)
self.view.addSubview(GMapView)
2) prepare marker
let bearing:Double = 90
var marker = GMSMarker()
marker.icon = UIImage(named: MARKER_IMAGE_NAME)
marker.appearAnimation = GMSMarkerAnimation.pop
3) apply rotation
marker.rotation = bearing
4) add marker to google map
marker.map = GMapView

How To Set a Restricted Region or Limited Region in Google Map,Swift

I have list of markers on my Googlemap,fixed all the markers.Now I need to get Limited Region only possible to scroll inside the limited Region.How its possible.anyone Please help me to fix it.
Here is My Center Location
let center = CLLocationCoordinate2DMake(11.250220, 75.781573)
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 18.0)
Start by defining two locations which specify the bounds of the region you want to display. These could be opposite corners of the bounding box, or just two locations, for example:
CLLocationCoordinate2D location1 =
CLLocationCoordinate2DMake(-33.8683, 151.2086); // Sydney
CLLocationCoordinate2D location2 =
CLLocationCoordinate2DMake(-31.9554, 115.8585); // Perth
If you have more than two points that you want to include, you could calculate the bounds of them yourself. This can also be done using GMSCoordinateBounds, for example:
GMSCoordinateBounds* bounds =
[[GMSCoordinateBounds alloc]
initWithCoordinate: CLLocationCoordinate2DMake(-33.8683, 151.2086) // Sydney
andCoordinate: CLLocationCoordinate2DMake(-31.9554, 115.8585)]; // Perth
bounds = [bounds including:
CLLocationCoordinate2DMake(-12.4667, 130.8333)]; // Darwin
CLLocationCoordinate2D location1 = bounds.southWest;
CLLocationCoordinate2D location2 = bounds.northEast;
Next, you need to get the size of the map view in points. You could use this:
float mapViewWidth = _mapView.frame.size.width;
float mapViewHeight = _mapView.frame.size.height;
Now you have the info necessary to calculate the camera position:
MKMapPoint point1 = MKMapPointForCoordinate(location1);
MKMapPoint point2 = MKMapPointForCoordinate(location2);
MKMapPoint centrePoint = MKMapPointMake(
(point1.x + point2.x) / 2,
(point1.y + point2.y) / 2);
CLLocationCoordinate2D centreLocation = MKCoordinateForMapPoint(centrePoint);
double mapScaleWidth = mapViewWidth / fabs(point2.x - point1.x);
double mapScaleHeight = mapViewHeight / fabs(point2.y - point1.y);
double mapScale = MIN(mapScaleWidth, mapScaleHeight);
double zoomLevel = 20 + log2(mapScale);
GMSCameraPosition *camera = [GMSCameraPosition
cameraWithLatitude: centreLocation.latitude
longitude: centreLocation.longitude
zoom: zoomLevel];
You can then initialize the map view with this camera, or set the map view to this camera.
For this code to compile, you will need to add the MapKit framework to your project, and then also import it:
#import <MapKit/MapKit.h>
Note that this code doesn't handle wrap-around if your coordinates span across the date line. For example if you tried using this code with Tokyo and Hawaii, instead of displaying an area of the Pacific, it will try to display almost the entire world. In portrait mode it's not possible to zoom out far enough to see Hawaii on the left and Tokyo on the right, and so the map ends up centred on Africa with neither location visible. You could modify the above code to handle the wrap-around at the date line if you wanted to.

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 :)

Get latitude and longitude of center of half of mapView gooleMap (GMS MapView) ios

I am using GMS MapView (Google maps for iOS). I have four corners of a mapView as well as center position (lat. & long.) of the mapView but actually I want to logically divide my mapView in two parts, i.e. top and bottom and then I want to get center point (lat. & long.) of both parts.
Please see the attached images (I want to find the location(lat & long) of RED Dots).
and
How can I do that?
Its so kind of you if you answer it.
Thank you.
Gerry
according to the image you provided you are dividing the screen in the middle.
try this:
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat middleOfScreenX = [UIScreen mainScreen].bounds.size.width/2;
CGFloat pointYouDivideScreen = [UIScreen mainScreen].bounds.size.height/2; // this is in order to divide the map exactly in the middle, you can change as you wish
CGPoint partOneCenterPoint = CGPointMake(middleOfScreenX, pointYouDivideScreen/2);
CLLocationCoordinate2D partOneCenter = [self.map.projection coordinateForPoint:partOneCenterPoint];
CGPoint partTwoCenterPoint = CGPointMake(middleOfScreenX, screenHeight - (screenHeight - pointYouDivideScreen)/2);
CLLocationCoordinate2D partTwoCenter = [self.map.projection coordinateForPoint:partTwoCenterPoint];
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = partOneCenter;
marker1.map = self.mapView;
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = partTwoCenter;
marker2.map = self.mapView;
the markers are placed in the center of each part (just to show the centers)
you can get lat/lng of each center with:
partOneCenter.latitude;
partOneCenter.longitude;
partTwoCenter.latitude;
partTwoCenter.longitude;

There's something like CameraUpdateFactory to 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];

Resources