简体   繁体   中英

How can I get user current location without using google maps and other maps?

I am developing weather app and when the app first launch I want to detect user current location but I have tried use google maps but they are asking credit card in order to get api key for google maps, unfortunately, I did not have a credit card. do you any other solutions or alternatives.

Call this function in your OnCreate

private void location() {
    Utils.loge(TAG, "location");
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            Constants.location = location;
            Utils.loge(TAG, "onLocationChanged : " + location.getLatitude() + ", " + location.getLongitude());
        }

        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
    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;
    }
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
}

and in your Manifest:

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

Simple Code In Java.

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    location();
}
private void location() {
    Log.e("@@l", "location");
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {

            Log.e("@@", "onLocationChanged : " + location.getLatitude() + ", " + location.getLongitude());
            updateLocation(location);
        }

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

        public void onProviderEnabled(String provider) {
        }

        public void onProviderDisabled(String provider) {
        }
    };

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED) {

        return;
        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
    }

        public void updateLocation ( Location location){


        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        try {
        List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);

        String address = " ";

        if (listAddresses != null && listAddresses.size() > 0) {

        if (listAddresses.get(0).getThoroughfare() != null) {

            address = listAddresses.get(0).getThoroughfare() + " ";
        }

        if (listAddresses.get(0).getLocality() != null) {

            address += listAddresses.get(0).getLocality() + " ";
        }

        if (listAddresses.get(0).getPostalCode() != null) {

            address += listAddresses.get(0).getPostalCode() + " ";
        }

        if (listAddresses.get(0).getAdminArea() != null) {

            address += listAddresses.get(0).getAdminArea();
        }
        }

        Log.i("Address",address);

        } catch (Exception e) {

        e.printStackTrace();

        }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM