如何找到当前城市

时间:2017-09-13 12:30:29

标签: android

我已经尝试了很多代码来查找当前城市,但没有人在设备和模拟器上工作....他们给我的位置为空

locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {

                        if (ActivityCompat.checkSelfPermission(this, 
  Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.

                        }
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }

请解释为什么这段代码给我null。

2 个答案:

答案 0 :(得分:1)

 private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
    String strAdd = "";
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        String city = addresses.get(0).getLocality();
        String postalCode = addresses.get(0).getPostalCode();
        String one3 = addresses.get(0).getSubLocality();
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder("");
            for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                strReturnedAddress
                        .append(returnedAddress.getAddressLine(i)).append(
                        "\n");
            }
            strAdd = strReturnedAddress.toString();
            et_longitude.setText("" + LONGITUDE);
            et_latitude.setText("" + LATITUDE);
            et_street_no.setText(address);
            // et_street_name.setText(one3);
            et_surburb.setText(postalCode);
            et_city.setText(city);
        } else {
            Log.e("", "My Current location address No Address returned!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("", "My Current location address No Address returned!");
    }
    return strAdd;

}

答案 1 :(得分:0)

mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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;
    }
    Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Log.e("TAG","Lat : " + location.getLatitude());
    Log.e("TAG","Lng : " + location.getLongitude());

通过这个,您可以获得当前的latlng