Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ilija.myapplication;
- import android.content.Context;
- 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.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- import static android.location.LocationManager.GPS_PROVIDER;
- public class MainActivity extends ActionBarActivity {
- private double latitude;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- LocationManager locationManager = (LocationManager)
- getSystemService(Context.LOCATION_SERVICE);
- MyLocationListener locationListener = new MyLocationListener();
- locationManager.requestLocationUpdates(
- GPS_PROVIDER, 5000, 10, locationListener);
- locationListener.getLocation(locationManager);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /*---------- Listener class to get coordinates ------------- */
- private class MyLocationListener implements LocationListener {
- @Override
- public void onLocationChanged(Location loc) {
- Toast.makeText(
- getBaseContext(),
- "Location changed: Lat: " + loc.getLatitude() + " Lng: "
- + loc.getLongitude(), Toast.LENGTH_SHORT).show();
- String longitude = "Longitude: " + loc.getLongitude();
- Log.v("a", longitude);
- String latitude = "Latitude: " + loc.getLatitude();
- Log.v("a", latitude);
- /*------- To get city name from coordinates -------- */
- String cityName = null;
- Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
- List<Address> addresses;
- try {
- addresses = gcd.getFromLocation(loc.getLatitude(),
- loc.getLongitude(), 1);
- if (addresses.size() > 0)
- System.out.println(addresses.get(0).getLocality());
- cityName = addresses.get(0).getLocality();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
- + cityName;
- }
- @Override
- public void onProviderDisabled(String provider) {}
- @Override
- public void onProviderEnabled(String provider) {}
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {}
- /**
- * Function to get the user's current location
- *
- * @return
- */
- public Location getLocation(LocationManager locationManager){
- double latitude = 0;
- double longitude = 0;
- Location location = null;
- try {
- // getting GPS status
- boolean isGPSEnabled = locationManager
- .isProviderEnabled(LocationManager.GPS_PROVIDER);
- Log.v("isGPSEnabled", "=" + isGPSEnabled);
- // getting network status
- boolean isNetworkEnabled = locationManager
- .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- Log.v("isNetworkEnabled", "=" + isNetworkEnabled);
- if (isGPSEnabled == false && isNetworkEnabled == false) {
- // no network provider is enabled
- } else {
- if (isNetworkEnabled) {
- location=null;
- Log.d("Network", "Network");
- if (locationManager != null) {
- location = locationManager
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- if (location != null) {
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- // if GPS Enabled get lat/long using GPS Services
- if (isGPSEnabled) {
- location=null;
- Log.d("GPS Enabled", "GPS Enabled");
- if (locationManager != null) {
- location = locationManager
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if (location != null) {
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- Toast.makeText(
- getBaseContext(),
- "Location changed: Lat: " + location.getLatitude() + " Lng: "
- + location.getLongitude(), Toast.LENGTH_SHORT).show();
- return location;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement