【问题标题】:Create Floating Marker Google Maps iOS创建浮动标记 Google Maps iOS
【发布时间】:2017-07-30 08:31:01
【问题描述】:

是否有任何想法如何创建可以选择其位置但保持在其中心视图的浮动标记,并且它的地图可以滚动? 我找到了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 或任何坐标)。

任何帮助或线索将不胜感激。非常感谢!

【问题讨论】:

    标签: ios objective-c google-maps google-maps-markers


    【解决方案1】:

    我不会全部写出来,但是:

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

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

    【讨论】:

    • 非常感谢@Daij-Djan!
    【解决方案2】:

    下面的代码应该对你有所帮助。

    // 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);
    }
    

    希望有帮助!

    谢谢,纳雷什。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        我会在 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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-18
          • 1970-01-01
          • 1970-01-01
          • 2016-02-08
          • 2019-08-11
          • 1970-01-01
          • 2018-02-03
          相关资源
          最近更新 更多