How to get coordinate center of gmsmapview in iphone - iphone

I'm using the new Google Maps SDK for iOS
Can I get the true coordinate form GMSMapView.center?
Now it returns a value of CGPoint, but it's not the true coordinate.
Thank and Regards.

Use the projection method - (CLLocationCoordinate2D)coordinateForPoint:(CGPoint)pointof the mapview to convert a point in the map to the geological coordinates
CGPoint point = mapView.center;
CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point];

I suggest to use [yourMapView.camera target] instead of Jing's solution.
Jing's solution work fine on iOS 6, but maybe having issue on iOS 7/7.1,
the projection may report wrong coordinate (a bit downward shift) !
I have checked the map bounds and padding is correct, both result of [projection pointForCoordinate: coordinate] and [projection coordinateForPoint:point] can contrast to each other, no ideas where the problem is...

Here is how to do it in Swift 4
let coordinate = mapView.projection.coordinate(for: mapView.center)

Are you looking for mapView.camera.target? This is a CLLocationCoordinate2D.

Swift 4
let point = mapView.center
let coordinate = mapView.convert(point, toCoordinateFrom: mapView)

Perhaps a better way if the user is allowed to interact with the map is using the map's did-become-idle delegate method. Here you can get the center coordinate after each camera change.
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
print(position.target)
}

Related

How to Place Bottom Of a Custom Annotation On a Specific Coordinate in Mapbox iOS

I'm using iOS Mapbox SDK in my app. I changed image for an annotation to a custom image (It looks like a map marker). When I add an annotation to a specific coordinate on the map view, It will be added but the center of my custom annotation image (the marker) will be set on the coordinate. I need to change the marker position to set the bottom of the marker on the coordinate. I found a way but I do not know is there a better way or not?
I converted the coordinate to a point, then changed the point y position, then converted the point to a new coordinate.
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let reuseIdentifier = "annotationImage"
var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: reuseIdentifier)
if annotationImage == nil {
annotationImage = MGLAnnotationImage(image: UIImage(named: "Orange")!, reuseIdentifier: reuseIdentifier)
}
return annotationImage
}
func addDestinationMarker(coordinate: CLLocationCoordinate2D) {
guard let mapView = mapView else { return }
if let annotations = mapView.annotations {
mapView.removeAnnotations(annotations)
}
var point = mapView.convert(coordinate, toPointTo: mapView)
point.y -= markerImageView.frame.height / 2
let newCoordinate = mapView.convert(point, toCoordinateFrom: mapView)
let annotation = MGLPointAnnotation()
annotation.coordinate = newCoordinate
mapView.addAnnotation(annotation)
}
I've run into this same issue and started to think that round map pins were becoming the defacto standard so they could just be plonked onto the map with the image centre denoting the coordinate. However if you take a look at this example on the Mapbox website, they use a non-round image and solve the offset problem quite nicely.
// The anchor point of an annotation is currently always the center. To
// shift the anchor point to the bottom of the annotation, the image
// asset includes transparent bottom padding equal to the original image
// height.
//
// To make this padding non-interactive, we create another image object
// with a custom alignment rect that excludes the padding.
image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: image.size.height/2, right: 0))
This does mean that you need to generate pin images that are twice as tall, with the lower half transparent, but that's really not a big deal.
You can solve this by leveraging the centerOffset property that MGLAnnotationView provides. Though I'm not sure if it's present in the MGLAnnotationImage you're using.
To set the anchor to the bottom of the annotation, use:
centerOffset.dy = -height / 2
If you set the frame beforehand, the height is simply frame.height.
The other answers express things correctly but both are missing the correct syntax:
annotationView?.centerOffset.y = -(annotationView?.frame.height ?? 0) / 2
This will achieve the expected result.

Set MKMapView Span without moving map

How do you adjust the MKMapView span (zoom) without moving the map location. I am trying to do this in an attempt to limit the user from zooming out further than preferable.
Use MKCoordinateSpanMake() method, for an instance
let span = MKCoordinateSpanMake(0.05, 0.05)
Try making the zoom with the MKMapView camera instead, adjusting eyeAltitude parameter can help you
func makeZoomWithCamera(){
let newCamera: MKMapCamera = MKMapCamera(lookingAtCenter: self.mapView.camera.centerCoordinate, fromEyeCoordinate: self.mapView.camera.centerCoordinate, eyeAltitude: 10)
self.mapView.setCamera(newCamera, animated: true)
}

Swift Converting CGMutablePath to MKPolygon

I have ann app which will allow users to draw in their own geofences. Right now the way that it works is the user draws their geofence on the screen and the app is keeping track of where they are drawing and storing their path into a CGMutablePath.
My question is how can I take this CGMutablePath and apply it to a MKPolygon so that the users hand drawn geofence can be applied as a Map overlay?
First you need to use your points from the user's touches to build an MKOverlay:
let coords = points.map({ CLLocationCoordinate2D(latitude: Double($0.x), longitude: Double($0.y)) })
let polygon = MKPolygon(coordinates: coords, count: coords.count)
You might need to also take into account the current zoom level of the map to make sure the conversion between points on the screen and lat/lng is correct.
Next, you can add this overlay to the map:
mapView.add(overlay)
To style the overlay (change its color, etc), use the MKMapViewDelegate method:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay: overlay)
renderer.fillColor = UIColor.blue
return renderer
}
My question is how can I take this CGMutablePath and apply it to a MKPolygon
You don't. If what you have is a general CGPath, use a custom MKOverlay along with MKOverlayPathRenderer.

Swift Spritekit. How am I able to lock a node in a particular point?

I am trying to figure out how to lock a SKSpriteNode in a particular y point. When my finger touches on the screen, the node displayed on the point where I touched. This is because inside TouchesBegan method, I wrote lines of code which are:
if isMovable == false {
isMovable = true
for touch in touches {
let location = touch.locationInNode(self)
ship.position = location
}
}
And then, I wrote these lines of code instead in order to get only the x point:
for touch in touches {
let location = touch.locationInNode(self)
ship.position = CGPointMake(location, self.frame.height/2 - 200)
}
However, the result I got was that location is not usable there because it is CGPoint, not CGFloat. I tried to cast it as CGFloat(location), but futile. Is there any way to lock a node in a y point?
The desirable function I want is when I touch anywhere on the screen, a y point is locked, meaning that a node is only movable horizontally, but not vertically movable.
Since my English is not good enough, I may lack missing some explanation that enables you guys to understand my problem more clearly. I appreciate if you understand me, and give me the solution! Thanks in advance.
Your location variable is of type CGPoint which is :
A structure that contains a point in a two-dimensional coordinate system.
Apple Documentation
So you could use that same variable location to get the xposition and lock the yto whatever point you choose:
for touch in touches {
let location = touch.locationInNode(self)
ship.position = CGPointMake(location.x, self.frame.height/2 - 200)
}

Get Location Marker in Google Maps SDK for native iOS/ swift

im working on an App for iOS where I embedded the new Google Map for native iOS. Everything works fine except one problem where I canĀ“t find a propper solution for native IOS/ swift neither here nor at google (if there is one please show me and sorry for annoying you)
I need get location fro new position when change marker location in map
How can I do that?
Thanks for advices
Finally solve using this function :
func mapView(mapViewUIView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D) {
marker.position.latitude = coordinate.latitude
marker.position.longitude = coordinate.longitude
println(marker.position.latitude)
let ULlocation = marker.position.latitude
let ULlgocation = marker.position.longitude
}