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];
}
}
Related
I have this code below for my new iOS Google Maps app. I can create dynamic markers on the map as the user taps on the screen but the polygon shape between the markers does not gets drawn. Any ideas why is this happening?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Controls whether the My Location dot and accuracy circle is enabled.
CGFloat currentZoom = 14.0f;
self.mapView.myLocationEnabled = YES;
//adds type of map: kGMSTypeSatellite, kGMSTypeTerrain, kGMSTypeHybrid, kGMSTypeNormal
self.mapView.mapType = kGMSTypeHybrid;
//Shows the compass button on the map
self.mapView.settings.compassButton = YES;
//Shows the my location button on the map
self.mapView.settings.myLocationButton = YES;
//Sets the view controller to be the GMSMapView delegate
self.mapView.delegate = self;
GMSCameraPosition *manhattan = [GMSCameraPosition cameraWithLatitude:40.790278
longitude:-73.959722
zoom:14];
self.mapView.camera = manhattan;
}
//setting up zoom
-(void)ZoominOutMap:(CGFloat)level
{
self.mapView.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.790218 longitude:-73.959722
zoom:level];
self.mapView.camera = camera;
}
-(void)zoomInMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom + 1;
[self ZoominOutMap:currentZoom];
}
-(void) zoomOutMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom - 1;
[self ZoominOutMap:currentZoom];
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarker *marker = [[GMSMarker alloc] init];
//adds animation for adding marker
marker.appearAnimation = kGMSMarkerAnimationPop;
//draw marker on tapped position
marker.position = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude);
marker.map = _mapView;
//set color of marker and make them draggable
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
[marker setDraggable: YES];
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}
In your code you are not providing rectangular path. Add proper polygon coordinates to draw GMSPolygon
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];
// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;
Update:
Answer How to insert array values to draw polygon based on long/lat values - Google Maps iOS?
You have to cut this line from 'didTapAtCoordinate' of code and paste it in viewDidLoad
#GMSMutablePath *rect = [GMSMutablePath path];#
Then it will work
I am trying to set up a way for the user to change the zoom of the map. Here's the code I have. What's the problem? Is there no implementation for invoking the zoom functionality? or is it that I set self.mapView.camera twice, in two different functions (viewDidLoad and ZoominOutMap)?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Controls whether the My Location dot and accuracy circle is enabled.
CGFloat currentZoom = 14.0f;
self.mapView.myLocationEnabled = YES;
//adds type of map: kGMSTypeSatellite, kGMSTypeTerrain, kGMSTypeHybrid, kGMSTypeNormal
self.mapView.mapType = kGMSTypeHybrid;
//Shows the compass button on the map
self.mapView.settings.compassButton = YES;
//Shows the my location button on the map
self.mapView.settings.myLocationButton = YES;
//Sets the view controller to be the GMSMapView delegate
self.mapView.delegate = self;
GMSCameraPosition *manhattan = [GMSCameraPosition cameraWithLatitude:40.790278
longitude:-73.959722
zoom:14];
self.mapView.camera = manhattan;
}
//setting up zoom
-(void)ZoominOutMap:(CGFloat)level
{
self.mapView.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.790218 longitude:-73.959722
zoom:level];
self.mapView.camera = camera;
}
-(void)zoomInMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom + 1;
[self ZoominOutMap:currentZoom];
}
-(void) zoomOutMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom - 1;
[self ZoominOutMap:currentZoom];
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarker *marker = [[GMSMarker alloc] init];
//adds animation for adding marker
marker.appearAnimation = kGMSMarkerAnimationPop;
//draw marker on tapped position
marker.position = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude);
marker.map = _mapView;
//set color of marker and make them draggable
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
[marker setDraggable: YES];
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}
You forget to add action to the buttons:
If buttons are added programatically :
[zoomOut addTarget:self
action:#selector(zoomOutMapView:)
forControlEvents:UIControlEventTouchUpInside];
[zoomOut addTarget:self
action:#selector(zoomInMapView:)
forControlEvents:UIControlEventTouchUpInside];
For Storyboard :
//zoom controls
- (IBAction)zoomOut:(id)sender {
currentZoom = currentZoom - 1;
[self zoomHandler:currentZoom];
}
- (IBAction)zoomIn:(id)sender {
currentZoom = currentZoom + 1;
[self zoomHandler:currentZoom];
}
- (void)zoomHandler:(CGFloat)level
{
self.mapView.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.790218 longitude:-73.959722 zoom:level];
self.mapView.camera = camera;
}
I want the GMSMarker to move to camera. this is my code
-(void)addMap {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:latitude
longitude:longitude
zoom:15];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CGRect newFrame = CGRectMake( 0, 0, [Util window_width],[Util window_height]);
mapView = [GMSMapView mapWithFrame:newFrame camera:camera];
mapView.delegate = self;
mapView.myLocationEnabled = YES;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.icon =[Util imageWithImage:[UIImage imageNamed:#"set_address.png"] scaledToSize:CGSizeMake(170, 65)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView;
[self.MapContentView addSubview:mapView];
}
I have a marker on the map, normally it is located at the center of the map if user does not scroll the map.
When the user scroll the map, I want marker to move to the new location, along with the camera , so it is always centered of the map .
I try the following code for it
-(BOOL) mapView:(GMSMapView *) mapView didTapMarker:(GMSMarker *)marker
{
[mapView animateToLocation:marker.position];
return YES;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
marker.position = camera.target;
marker.appearAnimation = kGMSMarkerAnimationPop;
NSLog(#"camera.target.latitude %f,%f",camera.target.latitude,camera.target.longitude);
latitude=camera.target.latitude;
longitude=camera.target.longitude;
}
You can try this:
- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture
{
[self recenterMarkerInMapView:mapView];
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
[self recenterMarkerInMapView:mapView];
}
- (void)recenterMarkerInMapView:(GMSMapView *)mapView
{
// Get the center of the mapView
CGPoint center = [mapView convertPoint:mapView.center fromView:self.view];
// Reset the marker position so it moves without animation
[mapView clear];
marker.appearAnimation = kGMSMarkerAnimationNone;
marker.position = [mapView.projection coordinateForPoint:center];
marker.map = mapView;
}
Swift 4. To move the selected marker to centre of mapView.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.animate(toLocation: marker.position)
return true
}
I am building an iOS app.I am using storyboards to build the screens and i have integrated google map in my project.
I want to get location where tap in the map and want to show that location in a label like in Airbnb app.I’m unable to do that,could someone help.
here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView_;
}
Help is appreciated!
You have to use the GMSMapViewDelegate protocol for that. Precisely, the method mapView:didTapAtCoordinate: will do what you require and detect a tap on the map and giving you the tap's coordinate.
To use the method, add the following line in your viewDidLoad:
mapView_.delegate = self;
Then in the same class implement the method:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
// coordinate contains your coordinate :)
NSLog(#"did tap at coordinate: (%f, %f)", coordinate.latitude, coordinate.longitude);
}
Update: To add a marker you can do the following:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate{
// coordinate contains your coordinate :)
NSLog(#"did tap at coordinate: (%f, %f)", coordinate.latitude, coordinate.longitude);
// Create the marker and add it to the map
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.map = mapView_;
// Zoom into the current location
GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithTarget:position zoom:15.0];
[mapView_ animateToCameraPosition:cameraPosition];
}
I am trying to make my google maps map view only half of the screen. I have resized the map view to be half the screen and also changed my code so it's only the bounds of the map view which is half the screen but it still goes full screen. Anyone got a solution?
// setting up mapView
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
_mapView = [GMSMapView mapWithFrame:_mapView.bounds 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;
// Creates a marker in the center of the map.
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(-36.86, 151.20);
marker2.title = #"Sydney2";
marker2.snippet = #"Australia2";
marker2.map = _mapView;
Thanks,
Curtis
You may can try to add the _mapView as a subview instead of assigning it to self.view.
[self.view addSubview:_mapView];
You can do it with the help of storyboard.
Drag a UIView in your ViewController and add GMSMapView class to the
UIView.
Set the frame size for your UIView using storyboard. Create outlet
for the UIView( name it mapView) and connect it.
In your ViewController class under viewDidLoad add these two lines
two view the google map.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: YOUR_LATITUDE
longitude: YOUR_LONGITUDE zoom:8];
[self.mapView setCamera:camera];
Add marker to the location using following code.
GMSMarker *marker = [[GMSMarker alloc]init];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(YOUR_LATITUDE, YOUR_LONGITUDE);