How to implement location search using google maps in ios? - ios

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];
}

Related

Not showing correct view after zooming on GMSMap

I have been using GMSMap in my app. I am showing current location in Map. But after zooming the Map it does not show city and state, also path is not visible in the map. I want to I have zooming on map and clear map in the present states and city's. Please assist me for my issue
I am used this code
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.6987 longitude:75.8817 zoom:0];
GMSMapView *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(22.6987, 75.8817);
marker.title = #"indore";
marker.snippet = #"India";
marker.map = mapView;
Now Show Look like please see ones

How to move marker and the camera synchronously and always place marker to centre of camera position IOS

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
}

Blue dot for current location not displaying using google maps for iOS

I'm trying to implement current locations in my app. Please see my code for the viewDidLoad below:
- (void)viewDidLoad {
[super viewDidLoad];
[self hideTabBar:self.tabBarController];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
mapView_.settings.compassButton = YES;
mapView_.settings.myLocationButton = YES;
mapView_.accessibilityElementsHidden = NO;
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_;
}
I can see the map in the iOS simulator (iPhone 4s) with the marker set to Sydney, but no blue dot to be seen anywhere. Is current location perhaps working but the UI element is hidden?
Thanks for any help/suggestions!
In my app I need to implement the CLLocationManager and set NSLocationWhenInUseUsageDescriptionin the Custom Target Properties.
Hope this helps.
In simulator, you need to set custom location like following:
Debug > Locations > Custom Location... then enter a latitude and longitude for the location you would like to simulate.
NOTE: if this method didn't work, then just select Freeway Drive instead of Custom Location. Once you clicked on Freeway Drive, then do the same step as above mentioned.
func didTapMyLocationButton(for mapView: GMSMapView) -> Bool {
//if want to change the camera position
if self.latDouble != 0.0 && self.longDouble != 0.0
{
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: self.latDouble, longitude: self.longDouble, zoom: 18.0)
self.mapView.camera = camera
}
self.mapView.isMyLocationEnabled = true
self.mapView.reloadInputViews()
return true
}

Google Maps iOS MapView

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

Google Maps iOS SDK - add badge

So i've got a google map in a small view on one of my main views, i'm trying to get a badge to show a location, like google show in their example.. But after ages of trying to get the map to show inside a resizable view (so i can display it within another view), the way i've done it doesn't appear to have a way of adding a badge.
This is my code so far:
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
// Create marker
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"Hello World";
self.map.camera = camera;
self.map being (in the .h file):
#property (weak, nonatomic) IBOutlet GMSMapView *map;
This is google's example:
// Google map
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
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_;
But i seem unable to combine the two
For any one struggling with a similar problem:
This is how i solved using a custom view with a google map + badge
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 longitude:151.20 zoom:6];
// set camera location to Novi Sad, Serbia :-)
[self.mymap setCamera:camera]; // set the camera to map
// 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 = mymap;

Resources