Change GMSCameraPosition zoom (iOS) - ios

I want to change GMSCameraPosition' zoom programmatically (Google Maps iOS SDK).
I tried to change:
mapView.Camera.Zoom=11;
but it's a "read-only" property and "setZoom:" does not exist.
How can I do that?

There is different way to do that.
You can use -animateToZoom: on your GMSMapView, or you can create a GMSCameraPosition and set the coordinate and zoom level and then use -animateToCameraPosition: or create a GMSCameraUpdate and then use -animateWithCameraUpdate:
GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:11.0];
[self.mapView animateToCameraPosition:cameraPosition];
or
GMSCameraUpdate *update = [GMSCameraUpdate zoomTo:11.0];
[self.mapView animateWithCameraUpdate:update];
or
[self.mapView animateToZoom:11.0];
Hope it helps.

Related

Centering custom marker GMSMapView

I am trying to position my custom marker in iOS(Objective-C) with Google Maps.
Map view is all showing up fine with marker, however, moving the map will take the marker with it as its fetching current location. What I am looking for is the have it set to center of the map container, which will then fetch coordinates of map pin and display in search bar.
I am thinking I am doing this in reverse? I added the customer marker as a UIImage of the map container however it still does not show. Should I be getting the location from the position of the map marker, and then reverse geocoding address from there.
- (void)addPins:(float)lat andLng:(float)lng andName:(NSString*)strName withAddress:(NSString*)strAddr
{
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(currentUserLocation.coordinate.latitude, currentUserLocation.coordinate.longitude);
NSLog(#"lat : %f, lng : %f", lat, lng);
marker.icon = [UIImage imageNamed:#"map_pin"];
marker.title = strName;
marker.snippet = strAddr;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
}
The user needs to be able to search and set their address, besides their current location.
Thanks
Use your mapView delegate and use the responder to re-center your pin and re-fire your geocoordinate lookup.
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
Edit: If GMS Maps does not have these delegates, the approach may work with using whatever [mapView didUpdate]callback they have documented.
You can set GMSCameraPosition for centering your marker
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:"YOUR LATITUDE"
longitude:"YOUR LONGITUDE"
zoom:centerZoom];
"YOUT GMSMAPVIEW" = [GMSMapView mapWithFrame:"YOUR VIEW".bounds camera:camera];

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

How to show google map using address in ios?

I am showing google map in my app using Google Maps iOS API. I am able to show a map by giving the longitude & latitude of the place.I am using following code to show the google map.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
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(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
Now i want to show the map for the custom address.Please tell me how to do that?
Refer below link, implement in your code and try it. It may help to solve your problem.
https://developers.google.com/maps/documentation/geocoding/intro
You have to use "Reverse GeoCoding".
The term geocoding generally refers to translating a human-readable address into a location on a map. The process of doing the opposite, translating a location on the map into a human-readable address, is known as reverse geocoding.
All you need to do is send lat and long and get the readable address.
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=API_KEY
for more info on "Reverse geo coding" read here.

Prevent scrolling outside map area on google map

Am actually dealing with the Google Maps Framework for iOS, and I want to block scrolling out side a giving area.
What I tried to do at first, implement the delegate method : - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position and compare the map position with the right/left corners.But what I did has many issues when scrolling or zooming.
Below an exemple of my implementation :
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
if (position.target.latitude > topLat) {
GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:topLat
longitude:position.target.longitude
zoom:position.zoom
bearing:220
viewingAngle:0];
[self.mapView animateToCameraPosition:goBackCamera];
}
if (position.target.latitude < bottomLat) {
GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:bottomLat
longitude:position.target.longitude
zoom:position.zoom
bearing:220
viewingAngle:0];
[self.mapView animateToCameraPosition:goBackCamera];
}
if (position.target.longitude > rightLong) {
GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
longitude:rightLong
zoom:position.zoom
bearing:220
viewingAngle:0];
[self.mapView animateToCameraPosition:goBackCamera];
}
if (position.target.longitude < leftLong) {
GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
longitude:leftLong
zoom:position.zoom
bearing:220
viewingAngle:0];
[self.mapView animateToCameraPosition:goBackCamera];
}
}
Do you no a way more efficient to deal with this?
PS: TopLat, RightLong ... mean top Latitude and Right longitude etc
Regards
One way I can think of is by setting up the bound in the map in you want maps to scroll using GMSCoordinateBounds.
Next thing you can do is to set up the scroll gesture to true just within that section of maps you have specified above bounds. For the rest of the map the scroll gesture should be false.
Use GMSCoordinateBounds(northeast co-ordinates, southwest co-ordinates) method.
This might be helpful
GMSCoordinateBounds Class Reference

change the default location on google maps iOS sdk

it is my first time to use google map and i asked to let the user choose the location that he wanted to point by taping on the map. when the application start it will point on the device location how the user will change it to the location he wants?
First get the point of user's tap. Then, get coordinate value for that CGPoint and set it as mapView's center.
-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
// Get tap point
CGPoint tapPoint = [recognizer locationInView:[recognizer superView]];
// Convert CGPoint to CLLocationCoordinate2D
CLLocationCoordinate2D center = [self.mapView.projection coordinateForPoint:tapPoint];
// Set camera of mapView
GMSCameraPosition * camera = [GMSCameraPosition cameraWithLatitude:center.latitude longitude:center.longitude zoom:self.mapView.camera.zoom];
[self.mapView setCamera:camera];
}
Alternatively you can implement the GMSMapViewDelegate and use the - mapView:didTapAtCoordinate: method.
You can move the view port by doing something similar like this to move the center point of map view
GMSCameraPosition *sydney = [GMSCameraPosition cameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
[mapView_ setCamera:sydney];
(Google Map SDK called it camera position, as if you use a camera to target a portion of the whole map)
Checkout https://developers.google.com/maps/documentation/ios/views?hl=en Moving the camera section
So here, in order to finish your task with move use to the point where he tapped. You'll need to do following steps:
define a iVar or property to keep record of the tap point location(lat/lng):
CLLocationCoordinate2D *currentTapLocation;
In didTapAtCoordinate delegate, fetch that position
- (void) mapView:(GMSMapView *) mapView didTapAtCoordinate:(CLLocationCoordinate2D) coordinate{
currentTapLocation = coordinate;
}
Create a GMSCamera and set camera to currentTapLocation
- (void) mapView:(GMSMapView *) mapView didTapAtCoordinate:(CLLocationCoordinate2D) coordinate{
currentTapLocation = coordinate;
GMSCameraPosition *newCameraPosition = [GMSCameraPosition cameraWithLatitude:currentTapLocation.latitude
longitude:currentTapLocation.longitude
zoom:6];
[mapView_ setCamera:newCameraPosition];
}
Optional step: if you want animate to the new position instead setCamera directly,
you can do:
[mapView_ animateWithCameraUpdate:[mapView_
setCamera:newCameraPosition];];
instead of
[mapView_ setCamera:newCameraPosition];

Resources