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];
Related
I am adding a simple marker to my mapView, like so:
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(40.763981, -73.971647);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = self.mapView;
marker.snippet = #"My Marker";
self.mapView.selectedMarker = marker;
As you can see I select the marker upon placement, however I want to keep this marker selected, showing the infoview, at all times it is on the map.
Currently, if I tap elsewhere on the map, the infoview is hidden (but the marker stays)
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.
I'm using GoogleMaps SDK in my iOS project.
myLocationEnabled property is set to YES.
I expect the user location pin hovering over all other pins like it does on Android. But for some reason it is overlapped with other map pins.
Googling didn't give me anybody with similar problem. What could I do wrong? I'm using standard GMSMapView workflow when adding pins on the map.
GMSMarker * marker = [[GMSMarker alloc]init];
CLLocationCoordinate2D coordinate;
//cluster - is just an object of business logic
coordinate.latitude = [cluster.latitude floatValue];
coordinate.longitude =[cluster.longitude floatValue];
[marker setIcon:markerIcon];
[marker setPosition:coordinate];
[marker setUserData:cluster];
[marker setMap:mapView_];
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];
I am building an iOS app using storyboards and Google Maps. Using iOS6
My application features the split view navigation as seen in the facebook app
On my left view I am selecting an item in a list which has lat/long cords and showing it on my map on the following method
- (void)viewWillAppear:(BOOL)animated
I would like to remove all markers in this method before I add another one (so only one marker is on the map), is there a way to do this? Below is my code to add a marker to the mapView
Thanks in advance - Jon
- (void)loadView
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:poi.lat
longitude:poi.lon
zoom:15];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
self.view = mapView;
mapView.mapType = kGMSTypeHybrid;
//Allows you to tap a marker and have camera pan to it
mapView.delegate = self;
}
-(void)viewWillAppear:(BOOL)animated
{
GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
options.position = CLLocationCoordinate2DMake(poi.lat, poi.lon);
options.title = poi.title;
options.snippet = poi.description;
options.icon = [UIImage imageNamed:#"flag-red.png"];
[mapView addMarkerWithOptions:options];
[mapView animateToLocation:options.position];
[mapView animateToBearing:0];
[mapView animateToViewingAngle:0];
}
To remove all markers
mapView.clear()
To remove a specific marker
myMarker.map = nil
To remove all markers simple do:
[self.mapView clear];
Please refer to the Google Map documentation: Google Maps SDK for iOS
Please refer to the section title "Remove a marker". Always check documentation for such methods.
mapView.clear()
// It will clear all markers from GMSMapView.
To remove a single marker from map.
yourMarkerName.map = nil
To remove all markers from map.
yourMapViewName.clear()
It's a little bit tricky here if you want to remove only one marker among a group of your markers.
let marker = GMSMarker(position: coordinate) //
marker.icon = UIImage(named: "ic_pin_marker")
marker.map = mapView
mapView.selectedMarker = marker // the specific marker you want to remove or modify by set it as selectedMarker on the map.
then you want to remove or modify
mapView.selectedMarker.map = nil //example to remove the marker form the map.
Hopefully useful with your condition.
mapView.clear() is not a good idea .
because The Places SDK for iOS enforces a default limit of 1,000 requests per 24 hour period.(If your app exceeds the limit, the app will start failing. Verify your identity to get 150,000 requests per 24 hour period.)
whit mapView.clear() the requests increase .
the best way is clear each marker and polylines .