【问题标题】:android google map current location not getting [duplicate]android google map当前位置没有得到[重复]
【发布时间】:2016-05-02 20:35:24
【问题描述】:

我正在从事一项谷歌地图活动。我正在尝试获取当前位置。它在模拟器上完美运行,但无法在我的手机上获取当前位置。这是我在我的应用程序中使用的代码...

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        double lat = (double) (location.getLatitude());
        double lng = (double) (location.getLongitude());

        Constants.lat = String.valueOf(lat);
        Constants.lng = String.valueOf(lng);
     }

Constants 是我在其中声明了经度和纬度静态变量的类。这段代码写在一个函数中,它是从 onCreate 方法调用的。我每次都得到空位置。我也在清单文件中声明了下面的代码。

     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

有人可以帮帮我吗?谢谢 。

【问题讨论】:

    标签: android google-maps location


    【解决方案1】:

    从文档中试用此代码。

        private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            mMap.setMyLocationEnabled(true);
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
    
    
             mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    
           @Override
           public void onMyLocationChange(Location arg0) {
            // TODO Auto-generated method stub
    
             mMap.addMarker(new MarkerOptions().position(new    LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
           }
          });
    
            }
        }
    }
    

    在您的 onCreate 中调用此函数。希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      相关资源
      最近更新 更多