创建浮动标记 Google 地图 iOS

Create Floating Marker Google Maps iOS

有没有想过如何创建可以选择其位置但保持在其中心视图中并且其地图可以滚动的浮动标记? 我找到了 javascript 版本

function initialize() {
  var crosshairShape = {
    coords: [0, 0, 0, 0],
    type: 'rect'
  };
  var latlng = new google.maps.LatLng(54.62279178711505, -5.895538330078125);
  var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.SATELLITE,
    mapTypeControlOptions: {
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
    }
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  var marker = new google.maps.Marker({
    map: map,
  });
  document.getElementById('coordinates').innerHTML = "<b>center coordinates</b>: " + map.getCenter().toUrlValue(6)
  google.maps.event.addListener(map, 'center_changed', function() {
    document.getElementById('coordinates').innerHTML = "<b>center coordinates</b>: " + map.getCenter().toUrlValue(6);
  });
  marker.bindTo('position', map, 'center');
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<div id="map_canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
<div id="coordinates"></div>

已经有一个应用程序实现了这一点,但我真的不知道该怎么做。这是图片

我的观点是,每个用户滚动地图,它的标记保持在中心视图然后我们可以获得它的坐标(GMSPlace 或任何坐标)。

如有任何帮助或线索,我们将不胜感激。非常感谢!

我不会全部写出来但是:

根本不要为此使用标记 ;) 只需使用添加为地图子视图的普通 UIView .. 居中。

中心坐标就是可见区域的中心坐标

下面的代码应该能帮到你。

// Call this function from -viewdidload with valid GMSMapView input.
- (UIImageView *)addMarkerOnMapViewCenter:(GMSMapView *)mapView
{
    UIView *vwMapParentView = mapView.superview;
    UIImageView *vwMarker = [UIImageView new];
    [vwMarker setFrame:(CGRect){0,0,20,40}];
    // Customize the Background color as per your need.
    [vwMarker setBackgroundColor:[UIColor greenColor]];
    // Drop an icon named markerIcon.png in your project bundle
    [vwMarker setImage:[UIImage imageNamed:@"markerIcon.png"]];
    [vwMarker setCenter:(CGPoint){vwMapParentView.center.x,(vwMapParentView.center.y - vwMarker.frame.size.height)}]; // if needed subtract navigation bar height as well as per the requirement.
    [vwMapParentView addSubview:vwMarker];
    [vwMapParentView bringSubviewToFront:vwMarker];
    return vwMarker;
}
// And then,Implement this GMSMapView's delegate method.
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
    CLLocationCoordinate2D target = position.target;
    NSLog(@"%f : %f",target.latitude,target.longitude);
}

希望对您有所帮助!

谢谢,Naresh。

正如Daij-Djan所说,不要添加为标记。只需使用普通 UIView/UIImageView 添加作为居中地图的子视图。然后实现此代码 Google 映射 didChangeCameraPositionidleAtCameraPosition 委托:

- (void) mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
{
    CGPoint point = mapView.center;
    CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point];
}

我会在 MapView 的中心添加一个图像,并在用户停止滚动时获取其坐标。

创建 ImageView:

UIImageView *pin = [[UIImageView alloc] initWithFrame(0,0,10,10)];
pin.center = self.view.center;
pin.image = [UIImage imageNamed:@"pin"];
[self.view addSubview:pin];
[self.view bringSubviewToFront:pin];

获取坐标:

-(void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position
{
CGPoint point = pin.center;
CLLocationCoordinate2D coor = [mapView.projection coordinateForPoint:point];
CGFloat longitude = coor.longitude;
CGFloat latitude = coor.latitude;
}