iOS Google Maps Sdk Gmsmarker Positioning

iOS google maps sdk GMSMarker positioning

Have a look at using GMSProjection. To shift the center of the map 100px from the markers location you would do something like:

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
CGPoint point = [mapView_.projection pointForCoordinate:marker.position];
point.x = point.x + 100;
GMSCameraUpdate *camera =
[GMSCameraUpdate setTarget:[mapView_.projection coordinateForPoint:point]];
[mapView_ animateWithCameraUpdate:camera];
return YES;
}

Position GMSMarker on bottom when clicked

mapView.selectedMarker = marker is missing in didTapMarker delegate method. The method should look like this:

func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {

var point = mapView.projection.pointForCoordinate(marker.position)
point.y = point.y - 150

let camera = GMSCameraUpdate.setTarget(mapView.projection.coordinateForPoint(point))
mapView.animateWithCameraUpdate(camera)
mapView.selectedMarker = marker
return true
}

How to set fixed Marker position in Google MapView iOS

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

How to update Google Maps marker position in swift/iOS

If you have any kind of unique identifier for your positions that came from server, you can keep a list of markers and then update their location when new data arrive. Something like this:

for result in json.arrayValue {

let lat = result["latitude"].doubleValue
let lon = result["longitude"].doubleValue

let identifier = result["identifier"] as! String
self.myMarkersDictionary[identifier]?.position.latitude = lat
self.myMarkersDictionary[identifier]?.position.longitude = lon
...
}

GMSMarker icon from center (iOS)

You can change the start position of your marker icon with the property groundAnchor.

Google Maps SDK for iOS documentation:

The ground anchor specifies the point in the icon image that is
anchored to the marker's position on the Earth's surface. This point
is specified within the continuous space [0.0, 1.0] x [0.0, 1.0],
where (0,0) is the top-left corner of the image, and (1,1) is the
bottom-right corner.

Example:

The below example rotates the marker 90°. Setting the groundAnchor
property to 0.5,0.5 causes the marker to be rotated around its center,
instead of its base.

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
CLLocationDegrees degrees = 90;
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.groundAnchor = CGPointMake(0.5, 0.5);
london.rotation = degrees;
london.map = mapView_;

iOS Google Maps SDK, Can't listen to GMSMarker tap event

I think you might need this, after you create the map:

mapView_.delegate = self;


Related Topics



Leave a reply



Submit