Here I'm working on mapKitView using GoogleMapKit and here i want to do when the user drag the marker by using of didBeginDragging, didDragging and didEndDragging
and release the marker at any location point of the screen. I found a helping content to fixed the marker in screen from Hugo Jordao's answer, but this only works to fixed marker position in the center of the screen only.So how can i set marker at any specific screen point??
Dragging Method
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
if marker == mapMarker{
self.markerPosition = GMSCameraPosition(latitude: marker.position.latitude, longitude: marker.position.longitude, zoom: 5.0)
print("End Dragging")
}
}
Set Marker center to the Screen
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if self.markerPosition == nil{
mapMarker.position = position.target
}else{
mapMarker.position = self.markerPosition.target
}
}
Answering my own question.
First added variable to store the location value in CGPoint
var markerPosition : CGPoint!
Then convert CLLocationCoordinate2D Location values into the CGPoint and store into markerPosition in didEndDragging marker method.
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
if marker == mapMarker{
self.markerPosition = self.mapView.projection.point(for: CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude))
print("End Dragging")
}
}
After that set GMSMarker position at any screen point as per CGPoint values into the MAPView
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if self.markerPosition == nil{
//To set pin into the center of mapview
mapMarker.position = position.target
}else{
//To set pin into the any particular point of the screen on mapview
let coordinate = self.mapView.projection.coordinate(for: self.markerPosition)
mapMarker.position = coordinate
}
}
Related
by tapping, it makes marker infinity. i want to one marker where I tapped. over and over again. not with lots markers.
I used MapView.clean(). but it delete every markers.
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
let marker = GMSMarker(position: coordinate)
marker.position.latitude = coordinate.latitude
marker.position.longitude = coordinate.longitude
print("hello")
print(markerr.position.latitude)
let ULlocation = markerr.position.latitude
let ULlgocation = markerr.position.longitude
print(ULlocation)
print(ULlgocation)
marker.map = self.mapView
}
Create the marker outside the didTapAt coordinate method and change it's coordinates in this method
class ViewController: UIViewController, GMSMapViewDelegate {
let marker = GMSMarker()
override func viewDidLoad() {
super.viewDidLoad()
marker.map = self.mapView
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D)
{
marker.position = coordinate
}
}
for some odd reason when a marker is tapped google map will not show the snippet window in my swift code. I just dont see what im doing wrong. I get the marker on the map but when i tap it it does not show anything. Thank you in advance.
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let position = place.coordinate
self.dismiss(animated: true) {
let marker = GMSMarker(position: position)
marker.title = "title here"
marker.snippet = "my snippet here"
marker.map = self.mapView
}
}
I had to remove the delegate below in order for the infowindow to show.
// func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// print("marker Tapped")
//
// return true
// }
You can put in the delegate
mapView.selectedMarker = marker
so the code will be as this :
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("marker Tapped")
mapView.selectedMarker = marker
return true
}
Hope this will help you.
I have been building a Google Maps iOS app and somehow my 'didTap marker' function does not work. If I tap on a marker on the map, the whole view simply just gets dragged off to left, but no code gets run. Otherwise, the print functions below would work. Where might cause this problem?
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker){
var lat: CLLocationDegrees = marker.position.latitude
var lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
print("markersArray is \(markersArray)")
print("formattedCoordinate is \(formattedCoordinate)")
markersArray = markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
}
You need to set delegate for mapview.
set delegate in viewDidLoad
myGMSMapView.delegate = self
then use this method
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
<#code#>
}
Add this delegate in your class.
GMSMapViewDelegate
Now, this will be your marker action.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
mapView.delegate = self
print("You tapped : \(marker.position.latitude),\(marker.position.longitude)")
}
I want to add and remove Marker(pin) in Google Maps.
I want to drop pin with long touch and remove it. I want to use it to select my destination. How can I do it?
let position = CLLocationCoordinate2DMake(10, 10)
let marker = GMSMarker(position: position)
marker.map = mapView
For the ones who're looking for a complete code snippet using Swift:
Implement Protocol GMSMapViewDelegate
Drag an Instance of GMSMapView #IBOutlet weak var googleMapView: GMSMapView!
Mention GMSMapView Delegate within viewDidLoad() as googleMapView.delegate = self
Implement the didTapAt delegate function:
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
googleMapView.clear() // clearing Pin before adding new
let marker = GMSMarker(position: coordinate)
marker.map = googleMapView
}
This code should help you!
//MARK: GMSMapViewDelegate Implimentation.
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
plotMarker(AtCoordinate: coordinate, onMapView: mapView)
}
//MARK: Plot Marker Helper
private func plotMarker(AtCoordinate coordinate : CLLocationCoordinate2D, onMapView vwMap : GMSMapView) {
let marker = GMSMarker(position: coordinate)
marker.map = vwMap
}
PS: Dont forget to confirm to GMSMapViewDelegate in ViewController and assign googleMap.delegate = self somewhere in viewDidLoad()
Hope that helps!
I'm playing with google maps and I need to override default behavior of tapping on the marker - by default marker goes to center of the screen and infowindow is being shown above. I need to figure out a way that when marker is tapped marker moves to the bottom of screen and infowindow shows on center. I found solutions here but I couldnt translate it to to swift
Any help please?
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
mapView.animateToLocation(marker.position)
mapView.selectedMarker = marker
var point = mapView.projection.pointForCoordinate(marker.position)
point.y = point.y - 200
var newPoint = mapView.projection.coordinateForPoint(point)
var camera = GMSCameraUpdate.setTarget(newPoint)
mapView.animateWithCameraUpdate(camera)
return true
}
add GMSMapViewDelegate
// move marker
func updateLocationoordinates(coordinates:CLLocationCoordinate2D) {
if marker == nil
{
marker = GMSMarker()
marker.position = coordinates
let image = UIImage(named:"destinationmarker")
marker.icon = image
marker.map = mapPageView
marker.appearAnimation = GMSMarkerAnimation.pop
}
else
{
CATransaction.begin()
// CATransaction.setAnimationDuration(0.1)
marker.position = coordinates
CATransaction.commit()
}
print(coordinates)
}
Camera change Position this methods will call every time
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
var destinationLocation = CLLocation()
destinationLocation = CLLocation(latitude: position.target.latitude, longitude: position.target.longitude)
destinationCoordinate = destinationLocation.coordinate
updateLocationoordinates(coordinates: destinationCoordinate)
}
good luck