Atheer Mohmmad
Atheer Mohmmad

Reputation: 1

How to determine location in android?

I am working in small project and the location will be part of the log in, so I want to determine location which is easier than writing location but there was an error how can fix this code please ?

public class RequestUs extends AppCompatActivity {

private Button bLocation;
private TextView tvCoordinate;
private LocationManager lm;
private LocationListener ls;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request_us);

    bLocation = (Button) findViewById(R.id.bLocation);
    tvCoordinate = (TextView) findViewById(R.id.tvCoordinate);
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    ls = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            tvCoordinate.append("\n" + location.getLatitude()+" "+ location.getLatitude());


        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);

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

        requestPermissions(new String[] {
                Manifest.permission.ACCESS_FINE_LOCATION ,
                Manifest.permission.ACCESS_COARSE_LOCATION ,
                Manifest.permission.INTERNET
        }, 10);
        return;
    }else
    {
      ConfigureButton();
    }



}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 10:
            if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                ConfigureButton();
    }

}

private void ConfigureButton() {

    bLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            lm.requestLocationUpdates("gps", 5000, 0, ls);



        }
    });

}

}

Upvotes: 0

Views: 296

Answers (1)

GomathiSelvakumar
GomathiSelvakumar

Reputation: 494

find location based on input(address)

public JSONObject getLocationInfo(String address) {     

    JSONObject json = null;
    try {

        address = address.replaceAll(" ", "%20");
        String url = "https://maps.google.com/maps/api/geocode/json?address="
                + address + "&sensor=true&language=en&key=AIzaSyA1pAWC5_88Xy8UpxMvojUTNt-fQqON4Xc";
        json = JSONParser.readJsonFromUrl(url);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return json;
}

public static JSONObject readJsonFromUrl(String url) throws IOException,
        JSONException {
    InputStream is = new URL(url).openStream();
    try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is,
                Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    } finally {
        is.close();
    }
}
private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

Upvotes: 1

Related Questions