Google Map's marker not being displayed properly - ios

The app I'm working on has a map which contains a circle on top that resembles the area that will be searched plus it shows the area in miles on top. In order to calculate the area covered, I need to find the radius of the circle after the map camera has changed (or in Apple maps terms, when the region changes). In order to test that the map is getting the right destination/length of the radius , i'm placing a marker at it's center and a marker at the boundaries of the circle. After that i calculate the radius and continue to calculate the area. I was working with Apple maps first and everything worked fine and now i switched to Google maps in order to use Places API and other API's. In Apple maps the deltaLongtiude and deltaLatitude from the region made it easy to calculate the changes when the region has been changed. In Google maps, they use Camera instead of Region. it has no deltaLatitude/Longitude (correct me if i'm wrong). I'm using the same equation here to calculate the distance using cross multiplication. When i zoom out of the map and drag the map, the center mark is placed correctly however the marker on the boundary is misplaced. Please find the code below of what I'm doing.
In viewDidLoad: i set up the map as follow:
GMSMarker *currentLocation = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(latitude, longitude)];
currentLocation.map = _map;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude+0.0075 longitude:longitude zoom:13];
_map.camera = camera;
[_map setMinZoom:1 maxZoom:13];
The 0.0075 is just to adjust the camera properly.
Now in order to view if the markers are placed correctly, i use the following delegate method:
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position{
GMSMarker *currentLocation = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(position.target.latitude-0.0075, position.target.longitude)];
currentLocation.map = _map;
GMSMarker *currentLocation2 = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(position.target.latitude-0.0075, position.target.longitude + ((0.0172*position.zoom)/13))];
currentLocation2.map = _map;
}
currentLocation places a marker in the center and currentLocation2 places a marker at the boundary. In order to place the marker correctly in case the user zoomed out, I do a cross multiplication. Since at zoom level 13 the boundary marker should be longitude+0.0172 from the center marker, i multiple 0.0172 by the new zoom level and divide it by 13 and add the value to he longitude. When i run the app and zoom out and drag the map, the boundary marker is misplaced. It's somehow has the same coordinates of the previous zoom level. DO i have to update anything in order to make it work or am i doing anything wrong?
to view more what i'm talking about here are pictures. The first show the map at zoom level 13. As you can see a marker in center and a boundary marker. In the second picture it's at a different zoom level and as you can see the boundary marker stays in same place and doesn't add a new one on the updated camera.

Related

How to include all markers on google maps BUT also always center to one of the coordinates

I have an app that is showing points of interest around the user and also the user's location. What I am struggling with is the following: How to include all the point of interest markers and also ALWAYS center the map around the user's marker.
I am aware of the method:
zoom(toInclude: [CLLocationCoordinate2D], animated: Bool) ,
but that method is missing the option to center the map around a specific marker / coordinates.
I have also tried setting up GMSCoordinateBounds to include all of my points of interests as well as userLocation and then calling GMSCameraUpdate.fit(bounds) to set the camera to include all of my markers, which basically achieves the same result as the zoom(toInclude:) function.
So I would like to always center a map around a specific location WHILE also zooming out to the point so that all of my POIs are included and displayed on the map.
You can set the GMSMapView's camera to the coordinates you want to show in center.
let camera = GMSCameraPosition.camera(withLatitude: UserLocation.coordinates.latitude, longitude: UserLocation.coordinates.longitude, zoom: 10.0)
self.mapView.animate(to: camera)

MKMapView - to keep user at center and cover route drawn on mapview on the screen

I'm trying to achieve as follows:
User should always be at the center of the screen on MKMapView.
Route is drawn on the map as user will move.
I know, i can calculate the region to cover all the tracked points on the screen.
But here's my problem:
When i calculate the MKCoordinateRegion and setting it, it just fits the region that is best fitting to the screen but as soon as i'm trying to place user at center, a part of the line drawn on the MKMapView goes out of the screen.
Can anybody face this problem or any suggestions to handle this specific case, any help will be highly appreciated.
Thanks in advance.
I have accomplished it as follows:
Calculate the distance of farthest point from the user's current location (or any point you want to keep at the center).
Calculate the region, with your center point(user's current location in my case) and double the distance calculated above and make a region using te following code:
CLLocationCoordinate2D loc = [myLocation coordinate];
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(loc, distance * 2, distance * 2);
Set the region on the MapView and the trail will be shown inside the screen keeping user's location at the center.
Thanks.

MKMapView - Is there a way to use MKUserTrackingModeFollow with a modified center/focus?

I have a MapView that takes up the full screen and a few elements toward the top of the screen that overlay the map. These top elements potentially cover the blue location marker.
The map is currently tracking fine with MKUserTrackingModeFollow, but I'd like to move the tracking focus down about an inch (obviously the metric will vary by zoom level) to ensure the marker isn't covered. Is there a way to do this with MKUserTrackingModeFollow?
I've considered moving the map with the top elements, but due to design constraints this isn't an option.
Instead of using the userTrackingMode of the MKMapView object to follow the user's location, you could set up a delegate of CLLocationManager to receive location tracking events, and with each location update event you receive, set the center coordinate of the map view, with your vertical offset added to the coordinate value.
You can use the various Map Kit functions and MKMapView methods to convert your offset from some fraction of the map view's bounds.height to a MKMapPoint or CLLocationDistance value.

How to restrict the google map view from panning beyond provided coordinate bounds?

I'm using Google maps sdk in ios.
I want to restrict the user from viewing other states than the states he is allowed to view. The allowed region to view on the map is given as coordinates (FarRight Latitude and Longitude), (Near Left Latitude and Longitude) and the (Center latitude and Longitude).
------X
---X---
X------
X marks the allowed map coordinates on the big map.
When the user pans the map to a coordinate outside this boundary then it has to be panned back to the old location.
But the problem is, In google maps SDK,
- (void)mapView:(GMSMapView*)mapView didChangeCameraPosition:(GMSCameraPosition*)position
The above method gives the position of the visible map coordinate. From the property "Position" we can get only the target center coordinate not the boundar coordinate. How to solve this issue ?
Thanks.

MKMapView zooms itself out

This is a small method of mine which takes the location manager's current location and focuses the map just a little bit above it to make room for some other subviews that I've added to the top of the map. If the span's lat/long deltas get too big (more than 1.0), every time the region is set, the span gets bigger and bigger until the map is zoomed out all the way, even without actually touching the map. I think it has something to do with reusing the map view's span, but I don't know exactly what's going on.
- (void)setRegion {
CLLocationCoordinate2D coord = locationManager.location.coordinate;
MKCoordinateSpan span = mapView.region.span;
coord.latitude += 0.002 * (span.latitudeDelta / 0.012523);
MKCoordinateRegion region = MKCoordinateRegionMake(coord, span);
[mapViewGlobal setRegion:region animated:animated];
}
From Apple's docs "If you want to change the center coordinate without changing the zoom level, use the setCenterCoordinate:animated: instead." MKMapView
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(BOOL)animated
MKMapView will pick a new region that nicely fits the to a redefined zoom level while still displaying your entire target region and each time yo move north the span changes and things spiral out of control.
Also, your way of working out how much to change the latitude by is a bit odd. Aside from doing a multiple and divide when one would do the trick, it'll only work at a certain latitude (they get stretched away from the equator) and a certain zoom level. You'd be better off working out how many pixels you need the map scrolled by (presumably half the height of your subviews) and then using this function to work out where you need to center the map.
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view
Another way would be to put the subviews outside of the MKMapView, but that'll affect your aesthetics.

Resources