在Google地图片段中重新定位MyLocation按钮

时间:2017-05-15 01:57:02

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

在Google Maps API for Android中,右上角有一个按钮,用于在地图中找到您。

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

enter image description here

2 个答案:

答案 0 :(得分:5)

我可以推荐两个选项。 但是,我强烈建议你使用第一个 - 更经典和简单:

  1. 使用Map Padding (推荐)
  2. 您可以通过设置角落的Padding来重新定位任何GoogleMap控件。在你的情况下,我会从顶部设置一些填充:

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

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

    1. 停用该按钮并创建一个(少推荐)
    2. 禁用它很容易:

      googleMap.getUiSettings().setMyLocationButtonEnabled(false)
      

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

      1. 我会创建一个FloatingActionButton ,与Google地图应用程序(example)上的内容类似。
        • 您可以从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
        

答案 1 :(得分: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);

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