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 theCGPoint
and store intomarkerPosition
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 perCGPoint
values into theMAPView
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
How to Add Frameworks into the Swift Project
Receive Signal from Beacon While App Is in the Background
Load Custom Error HTMLstring When Wkwebview Loadrequest Fails
How to Show Viewcontroller from a Non-Viewcontroller Helper Class
Xcode 8/Swift 3 and Installing Apps on iPhone 4 Running iOS 7
How to Pair And/Or Bond to Ble on iOS Using Swift Code and an Hm-10 So Data Sent Is Encrypted
How to Convert Cgpoint in Nsvalue in Swift
How to Animate Uilabel's Textcolor Change
Hkanchoredobjectquery Not Returning Reliably in Background
How to Use Indexesofobjectspassingtest: in Swift
Swift 3 Weird Crashes (Type Inference)
Camera Only Takes Pictures in Portrait Mode Swift
Didreceiveremotenotification Not Called in Background Mode
Receipt Validation in iOS Returns Incorrect Info During Sandbox Testing
Swift 3: Transfer Utility Enumeratetoassignblocks Method Signature
Call Extensiondelegate to Create/Refresh Data for Complication