如何在 iOS 中随着 Google 地图的移动移动标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37992224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I move marker along with moving of Google Map in iOS?
提问by Shilpashree MC
I am displaying a marker in a particular place, along with displaying the current address in the address label on Google Maps.
我在特定位置显示标记,同时在 Google 地图的地址标签中显示当前地址。
Now, I want to change the location by moving the Google Map, but the problem is that when I am moving the map, I should simultaneously move the marker along with the map, and I should display the address of that location in the address label.
现在,我想通过移动谷歌地图来改变位置,但问题是当我移动地图时,我应该随着地图同时移动标记,并且我应该在地址标签中显示该位置的地址.
How can I do that?
我怎样才能做到这一点?
I tried this:
我试过这个:
let destinationMarker = GMSMarker(position: self.destinationLocation.coordinate)
let image = UIImage(named:"sourcemarker")
destinationMarker.icon = image
destinationMarker.draggable = true
destinationMarker.map = self.viewMap
//viewMap.selectedMarker = destinationMarker
destinationMarker.title = "hi"
destinationMarker.userData = "changedestination"
func mapView(mapView: GMSMapView, didEndDraggingMarker marker: GMSMarker)
{
if marker.userData as! String == "changedestination"
{
self.destinationLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
self.destinationCoordinate = self.destinationLocation.coordinate
//getAddressFromLatLong(destinationCoordinate)
}
}
回答by Shilpashree MC
// UpdteLocationCoordinate
func updateLocationoordinates(coordinates:CLLocationCoordinate2D) {
if destinationMarker == nil
{
destinationMarker = GMSMarker()
destinationMarker.position = coordinates
let image = UIImage(named:"destinationmarker")
destinationMarker.icon = image
destinationMarker.map = viewMap
destinationMarker.appearAnimation = kGMSMarkerAnimationPop
}
else
{
CATransaction.begin()
CATransaction.setAnimationDuration(1.0)
destinationMarker.position = coordinates
CATransaction.commit()
}
}
// Camera change Position this methods will call every time
func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
let destinationLocation = CLLocation()
if self.mapGesture == true
{
destinationLocation = CLLocation(latitude: position.target.latitude, longitude: position.target.longitude)
destinationCoordinate = destinationLocation.coordinate
updateLocationoordinates(destinationCoordinate)
}
}
回答by Hugo Jordao
Update for Swift 4:
Swift 4 更新:
First you need to conform with the GMSMapViewDelegate:
首先你需要符合GMSMapViewDelegate:
extension MapsVC: GMSMapViewDelegate{
And then set your VC as the delegate of viewMap in the viewDidLoad()
然后在 viewDidLoad() 中将您的 VC 设置为 viewMap 的委托
viewMap.delegate = self
After that you just need to use the following method to get updates from the camera position and set that as the new position for the marker:
之后,您只需要使用以下方法从相机位置获取更新并将其设置为标记的新位置:
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
destinationMarker.position = position.target
print(destinationMarker.position)
}
回答by Bharat
There is one trick that can help you out here. Instead of using a GMSMarker here, put an image pointing to the center, over your Google MapView.
这里有一个技巧可以帮助你。在这里不要使用 GMSMarker,而是在您的 Google MapView 上方放置一个指向中心的图像。
You can easily find the coordinates of Map's center using this :
您可以使用以下命令轻松找到地图中心的坐标:
double latitude = mapView.camera.target.latitude;
double longitude = mapView.camera.target.longitude;
Or this
或这个
GMSCoordinateBounds *bounds = nil;
bounds = [[GMSCoordinateBounds alloc] initWithRegion: visibleRegion];
CLLocationCoordinate2D centre = CLLocationCoordinate2DMake(
(bounds.southWest.latitude + bounds.northEast.latitude) / 2,
(bounds.southWest.longitude + bounds.northEast.longitude) / 2);
Now you can get the location address by using Geocoding API by google.
现在您可以使用谷歌的地理编码 API 获取位置地址。
Here is the reference : https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_geocoder
这是参考:https: //developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_geocoder
You can refresh Address when this delegate method is called :
您可以在调用此委托方法时刷新地址:
- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
Hope this helps.
希望这可以帮助。
回答by Teyam
Based from Release Notesmade in Maps SDK for IOS, changing a markers position will cause the marker to animate to the new location.
根据Maps SDK for IOS 中的发行说明,更改标记位置将导致标记动画到新位置。
To resolve this, you may use new features for GMSMarker as stated in Release Version 1.5such as:
要解决此问题,您可以使用版本 1.5 中所述的 GMSMarker 新功能,例如:
- Markers can be made draggable using the draggable property, and new drag delegate methods have been added to GMSMapViewDelegate. (Issue 4975)
- Added GMSMarkerLayer, a custom CALayer subclass for GMSMarker that supports animation of marker position and rotation. (Issue 4951, Issue 5743)
- 可以使用 draggable 属性使标记可拖动,并且新的拖动委托方法已添加到 GMSMapViewDelegate。(第 4975 期)
- 添加了 GMSMarkerLayer,这是 GMSMarker 的自定义 CALayer 子类,支持标记位置和旋转的动画。(第 4951 期,第 5743 期)
In addition to that, this post in GitHub - GoogleMapsAnimationGlitchand this SO post - How to smoothly move GMSMarker along coordinates in Objective cmight also help.
除此之外,GitHub 中的这篇文章 - GoogleMapsAnimationGlitch和这篇 SO 文章 -如何沿目标 c 中的坐标平滑移动 GMSMarker也可能有所帮助。