【问题标题】:Re-position MyLocation button inside Google Map fragment [duplicate]在谷歌地图片段中重新定位 MyLocation 按钮 [重复]
【发布时间】:2017-05-15 01:57:02
【问题描述】:

在适用于 Android 的 Google Maps API 中,右上角有一个按钮,用于在地图内定位您。

我想知道是否可以重新定位默认在 Android 版 Google Maps API 中的这个图标,因为我计划在顶部添加一个 EditText,就好像它是浮动的一样。

【问题讨论】:

    标签: android google-maps google-maps-android-api-2


    【解决方案1】:

    我可以建议 2 个选项。 但是,我强烈建议您使用第一个 - 更加经典和简单:


    1. 使用Map Padding(推荐)

    您可以通过从角落设置Padding 来重新定位任何 GoogleMap 控件。在你的情况下,我会从顶部设置一些填充:

    googleMap.setPadding(0, numTop, 0, 0); //numTop = padding of your choice

    它还会相应地更改地图相机的中心位置,这对于这种用例(添加标题/其他浮动控件)非常有用。


    1. 禁用按钮并创建您自己的按钮(不太推荐)

    禁用它很容易:

    googleMap.getUiSettings().setMyLocationButtonEnabled(false)
    

    但是,创建一个新的会比较棘手 - 主要是因为很难设置一个全功能的。

    1. 我会创建一个类似于 Google 地图应用程序 (example) 上的 FloatingActionButton
      • 您可以从here(称为“我的位置”)获取图标
      • Material Grey 300的Fab上色(可能是400,暂时不记得了)
    2. 定义一个 onClick 事件,将相机移动到用户的当前位置(为此,您将不得不使用 Location Service

      示例:

      //Acquire a reference to the system Location Manager
      LocationManager locationManager = 
           (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      
      //Acquire the user's location
      Location selfLocation = locationManager
           .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
      
      //Move the map to the user's location
      LatLng selfLoc = new LatLng(selfLocation.getLatitude(), selfLocation.getLongitude());
      CameraUpdate update = CameraUpdateFactory.newLatLngZoom(selfLoc, 15);
      googleMap.moveCamera(update);
      
    3. 如果您注意到,当您单击“我的位置”按钮时,它会开始跟踪您并相应地移动相机。为了创建这种效果,您需要覆盖googleMap.onCameraMovegoogleMap.onCameraIdle,并为您的应用程序编写代码,以便每当相机空闲时,地图将继续跟随用户,而每当用户移动相机时,它就会停止.

      onCameraIdle 的示例:

      //Acquire a reference to the system Location Manager
      LocationManager locationManager = (LocationManager)
                          getSystemService(Context.LOCATION_SERVICE);
      
      //Acquire the user's location
      Location selfLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
      LatLng cameraLocation = googleMap.getCameraPosition().target;
      
      float[] results = new float[3];
      Location.distanceBetween(selfLocation.getLatitude(), selfLocation.getLongitude(), cameraLocation.latitude, cameraLocation.longitude, results);
      
      if (results[0] < 30) //30 Meters, you can change that
          googleMap.moveCamera(...) //Move the camera to user's location
      

    【讨论】:

      【解决方案2】:
          View locationButton = ((View) findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
          locationButton.setVisibility(View.GONE);
          RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
          // position on right bottom
          rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
          rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
          rlp.setMargins(0, 180, 180, 0);
      

      您可以在可能找到“我的位置”按钮的位置设置边距。

      【讨论】:

      • 不适用于某些设备
      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      相关资源
      最近更新 更多