Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

geolocation - Network location provider not giving location android

I am developing a small android application in which I want to find out the user's current location by using the network provider. I tried this in following ways but it's not giving me any output :

networklocationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener networklocationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        Log.i("********************************",
                "this is my network location " + location);
        String Location_text = "NETWORK LOCATION latitude:"
                + location.getLatitude() + " longitude:"
                + location.getLatitude();
        network_location.setText(Location_text);
    }

    public void onStatusChanged(String provider, int status,
            Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location
// updates
networklocationManager
        .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                networklocationListener);

I gave permissions in my manifest file like this

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

Is there any thing which I am missing ? Is this the correct way? Need help. Thank you...


public class MainActivity extends Activity implements LocationListener {

    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager gpslocationManager;
    private LocationManager networklocationManager;
    private LocationManager networklocationManager1;
    private String provider;
    private TextView gps_location;
    private TextView network_location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gps_location = (TextView) findViewById(R.id.gps_location);
        network_location = (TextView) findViewById(R.id.network_location);
        networkLocation();
    }

    public void networkLocation() {
        networklocationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        LocationListener networklocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                Log.i("********************************",
                        "this is my network location " + location);
                String Location_text = "NETWORK LOCATION latitude:"
                        + location.getLatitude() + " longitude:"
                        + location.getLatitude();
                network_location.setText(Location_text);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };
        networklocationManager
                .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                        networklocationListener);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make sure you enable Location Services in Settings! That should be the problem. It might be disabled (and this setting will usually be found in Location and Security in Settings)

Let me know if it works!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...