0

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.

  • 1
    I think that location service will work without maps and you can get LatLng of user, and for get adress you can try this [link](https://stackoverflow.com/questions/9409195/how-to-get-complete-address-from-latitude-and-longitude) – Vadim Eksler Aug 30 '18 at 07:28
  • use googleapi client to fetch current location and updates – Quick learner Aug 30 '18 at 07:32
  • https://www.journaldev.com/13347/android-location-google-play-services – Quick learner Aug 30 '18 at 07:33
  • https://www.androidhive.info/2015/02/android-location-api-using-google-play-services/ – Quick learner Aug 30 '18 at 07:33
  • Hello. In addition to the links provided by _Quick learner_, here is the official Developer Guide : https://developer.android.com/training/location/ – Bruno Aug 30 '18 at 08:04
  • By using fusedApi you can easily get current location https://developers.google.com/location-context/fused-location-provider/ – Chetan Shelake Aug 30 '18 at 08:06

2 Answers2

1

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" />
0

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();

        }
        }
    }
Sandeep Pareek
  • 1,636
  • 19
  • 21