【问题标题】:map.setmylocationenabled(true) not workingmap.setmylocationenabled(true)不工作
【发布时间】:2016-04-09 02:10:45
【问题描述】:

我正在我的 Android 应用中使用 Google 地图。我需要将地图重新​​定位到客户的当前位置。我使用了以下语句 -

map.setmylocationenabled(true);

这会在右上角显示一个按钮,但单击该按钮不起作用。

按钮点击监听器:

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                    mMap.addMarker(new MarkerOptions().position(myLatLng).title("My Location"));
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(myLatLng, zoomLevel));
                    return false;
                }
            });

【问题讨论】:

  • 这只是启用按钮(它也默认启用)。您应该使用 FusedLocationProviderApi 获取位置,然后将地图相机移动/缩放到当前位置,示例请参见此处:stackoverflow.com/a/34582595/4409409
  • 尝试在该位置设置标记,然后您将获得标记的点击事件。
  • @DanielNugent 我在第一次启动地图时将地图居中。当我在地图上移动并想通过单击该按钮来重新定位屏幕上的当前位置时,问题就出现了。
  • @VenuSaini 显示你当前的按钮点击监听代码
  • 糟糕!我错过了按钮点击监听器。愚蠢的!感谢@DanielNugent 抽出宝贵时间 =]

标签: android google-maps location google-maps-markers


【解决方案1】:

最后一行是我的解决方案:

myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);

【讨论】:

    【解决方案2】:

    只需从my other answer here 获取代码,并修改您的按钮单击侦听器以请求另一个位置:

             mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                    @Override
                    public boolean onMyLocationButtonClick() {
                         if (mGoogleApiClient != null) {
                             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                         }
                         return false;
                    }
                });
    

    onLocationChanged() 中的代码将重新居中相机位置,然后再次取消注册位置更新:

    @Override
    public void onLocationChanged(Location location)
    {
        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }
    
        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
    
        //move map camera
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(11));
    
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }
    }
    

    【讨论】:

    • onMyLocationButtonClick in setOnMyLocationButtonClickListener 甚至不要打电话!既不是com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener() 也不是com.androidmapsextensions.GoogleMap.OnMyLocationButtonClickListener()
    • @Mr.Hyde 它应该可以工作。官方文档见这里:developers.google.com/android/reference/com/google/android/gms/…
    【解决方案3】:

    你有没有试过得到你的纬度和经度,使用后 setmylocationenabled(true)?

    例子

    gMap.setMyLocationEnabled(true);
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    gMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    

    您现在可以使用您的纬度和经度,并将相机动画到您获得的纬度/经度位置。希望对您有所帮助。

    【讨论】:

    • 做到了,但没有区别
    【解决方案4】:

    你应该添加setMyLocationEnabled (boolean enabled)

    启用或禁用我的位置层。

    启用且位置可用时,my-location 层 持续绘制用户当前位置的指示,并 轴承,并显示允许用户与之交互的 UI 控件 他们的位置(例如,启用或禁用相机跟踪 它们的位置和方位)。

    为了使用 my-location-layer 功能,您需要请求 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 的权限 除非您设置了自定义位置源。

    演示

    您应该在onMapReady (GoogleMap googleMap) 部分添加此内容。

     @Override
            public void onMapReady(GoogleMap googleMap) {
    
                googleMapOBJ = googleMap;
                if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                googleMapOBJ.setMyLocationEnabled(true);
                googleMapOBJ.getUiSettings().setMyLocationButtonEnabled(true);
    

    【讨论】:

      【解决方案5】:

      科特林方式

      mMap = googleMap
      mMap.isMyLocationEnabled = true
      mMap.uiSettings.isMyLocationButtonEnabled = true
      

      【讨论】:

        【解决方案6】:

        科特林-

        mMap!!.isMyLocationEnabled = true
        mMap!!.uiSettings.isMyLocationButtonEnabled =  true
        

        Java -

        myMap.setMyLocationEnabled(true);
        myMap.getUiSettings().setMyLocationButtonEnabled(true); 
        

        【讨论】:

          【解决方案7】:

          位置数据层在地图的右上角添加了一个我的位置按钮。当用户点击按钮时,地图以设备位置为中心。如果设备静止,则位置显示为蓝点,如果设备正在移动,则位置显示为蓝色 V 形。

          ma​​p.setMyLocationEnabled(Boolean) 调用需要权限,代码应明确检查权限是否可用。

          或者,您可以使用注释来隐藏丢失的位置权限。

          您可以使用新的Activity Result API 轻松实现此目的

          这里是代码

          class MapsActivity : AppCompatActivity() , OnMapReadyCallback {
          
          private lateinit var map: GoogleMap
          
          
          //suppress missing permission
          @SuppressLint("MissingPermission")
          private val locationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) {
          
              isGranted ->
          
              if (isGranted) {
          
                  //add my Location Button on top-right side corner
                  map.isMyLocationEnabled = true
              }
              else {
          
                  ActivityCompat.requestPermissions(this ,
                          arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION) ,
                          0)
              }
          }
          

          ActivityResultCallback 定义了应用如何处理用户对权限请求的响应。

          要显示系统权限对话框,请在 ActivityResultLauncher 实例上调用 launch() 方法。

          override fun onMapReady(googleMap: GoogleMap) {
              map = googleMap
          
              //call launch() passing in the permission required
              locationPermission.launch(Manifest.permission.ACCESS_FINE_LOCATION)}
          

          调用launch()后,系统权限对话框出现。当用户做出选择时,系统异步调用ActivityResultCallback的实现。

          【讨论】:

            【解决方案8】:

            我在我的真实设备中遇到了同样的问题,即使是我 add permission 的位置。仍然,它不起作用,我只是手动打开我设备中的位置它完美地工作。

            【讨论】:

            • “手动开启位置”是什么意思?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-04-26
            • 2018-07-10
            • 2019-05-02
            • 2018-04-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多