Gps android java координаты

Работаем с GPS в Android на Java

Мобильные устройства часто используются для решения различных задач связанных с определением географических координат. Транспорт, строительство, путешественники, так или иначе, нуждаются в определении своего местоположения или других объектов.

На сегодняшний день самым простым решением для этого является использование портативных приёмников спутниковых навигационных систем, в частности встраиваемых в устройства на базе Android. При этом наиболее распространёнными являются устройства с поддержкой системы GPS.

В Android SDK весь функционал по работе с навигационными системами объединён в пакет android.location. Ключевые компоненты данного пакета:

  • LocationManager – (класс) обеспечивает доступ к системной службе определения местоположения Android;
  • LocationListener — (интерфейс) регламентирует обработку приложение событий службы определения местоположения Android;
  • Location – (класс) представляет географические координаты полученные от навигационной системы.
Подготовка к работе

При написании Android приложения работающего с навигационными системами на Java с помощью Android SDK вначале необходимо выполнить ряд подготовительных операций.

Это связано с тем, что в отличие от Delphi, здесь отсутствуют какие-либо разрешения, предоставляемые по умолчанию и нет готовых компонентов, которые полностью брали бы на себя всю работу по взаимодействию с GPS приёмником.

Все необходимые действия потребуется выполнить самостоятельно.

Первым делом предоставляем приложению необходимые разрешения в файле манифеста.

Далее создаём в коде приложения объект LocationListener для обработки событий службы определения местоположения Android.

Назначение его методов – обработка соответствующих событий. Конкретно:

  • onLocationChanged – изменение местоположения. Именно он используется для определения текущих географических координат;
  • onStatusChanged – изменение состояния поставщика данных о местоположении. В частности приёмника GPS;
  • onProviderEnabled – получение доступа к поставщику данных о местоположении;
  • onProviderDisabled – потеря доступа к поставщику данных о местоположении.

В завершение зарегистрируем созданный нами объект LocationListener для работы с приёмником GPS.

Для этого создаём экземпляр класса LocationManager.

И выполняем регистрацию при помощи метода requestLocationUpdates.

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

Перед вызовом метода requestLocationUpdates обязательно необходимо проверить наличие соответствующих разрешений (оператор if). Если они отсутствуют перед оператором return можно выполнить некоторые действия. Например, записать сообщение об ошибке в журнал. Однако в любом случае при отсутствии необходимых разрешений работа с навигационной системой должна быть завершена до регистрации объекта LocationListener.

Метод requestLocationUpdates имеет несколько перегрузок. Наиболее часто используемая из них принимает четыре параметра. Именно она использована в примере выше.

  1. Поставщик данных о местоположении.
    В данном примере используется GPS;
  2. Минимальный интервал обновления данных о местоположения в миллисекундах.
    Значение «0» соответствует использованию минимально возможного интервала времени для данного устройства;
  3. Минимальное расстояние для обновления данных о местоположении в метрах.
    Значение «0» соответствует использованию минимально возможного расстояния для данного устройства;
  4. Регистрируемый объект LocationListener.

После регистрации приложение сможет получать информацию о местоположении устройства по мере его изменения.

Если необходимо получить её единовременно, необходимо вместо метода requestLocationUpdates использовать метод requestSingleUpdate, который также имеет несколько перегрузок.

Наиболее востребованная из них принимает три параметра:

  1. Поставщик данных о местоположении.
    В данном примере используется GPS;
  2. Регистрируемый объект LocationListener;
  3. Объект, реализующий обратный вызов.
    Необязательный параметр.

Пример использования метода requestSingleUpdate:

Источник

Android Location API to track your current location

Android Location API to track your current location

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Android Location API can be used to track your mobile current location and show in the app. In this tutorial, we’ll develop an application that fetches the user’s current location programmatically.

Android Location API

  • android.location.LocationListener : This is a part of the Android API.
  • com.google.android.gms.location.LocationListener : This is present in the Google Play Services API. (We’ll look into this in the next tutorial)

Android Location Services is available since Android API 1. Google officially recommends using Google Play Location Service APIs. Android Location Services API is still used to develop location-based apps for devices that don’t support Google Play Services.

LocationListener

The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services. The LocationListener class needs to implement the following methods.

  • onLocationChanged(Location location) : Called when the location has changed.
  • onProviderDisabled(String provider) : Called when the provider is disabled by the user.
  • onProviderEnabled(String provider) : Called when the provider is enabled by the user.
  • onStatusChanged(String provider, int status, Bundle extras) : Called when the provider status changes.

The android.location has two means of acquiring location data:

  • LocationManager.GPS_PROVIDER: Determines location using satellites. Depending on the conditions, this provider may take a while to return a location fix
  • LocationManager.NETWORK_PROVIDER: Determines location based on the availability of nearby cell towers and WiFi access points. This is faster than GPS_PROVIDER

In this tutorial, we’ll create a Service that implements the LocationListener class to receive periodic location updates via GPS Providers or Network Providers.

Android Location API Project Structure

android location api, android gps location

The project consists of a MainActivity.java class which displays a Get Location and a LocationTrack.java Service class.

Android Location APICode

The activity_main.xml layout is defined below.

The MainActivity.java class is defined below.

package com.journaldev.gpslocationtracking; import android.annotation.TargetApi; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import static android.Manifest.permission.ACCESS_COARSE_LOCATION; import static android.Manifest.permission.ACCESS_FINE_LOCATION; public class MainActivity extends AppCompatActivity < private ArrayList permissionsToRequest; private ArrayList permissionsRejected = new ArrayList(); private ArrayList permissions = new ArrayList(); private final static int ALL_PERMISSIONS_RESULT = 101; LocationTrack locationTrack; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); permissions.add(ACCESS_FINE_LOCATION); permissions.add(ACCESS_COARSE_LOCATION); permissionsToRequest = findUnAskedPermissions(permissions); //get the permissions we have asked for before but are not granted.. //we will store this in a global list to access later. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < if (permissionsToRequest.size() >0) requestPermissions(permissionsToRequest.toArray(new String[permissionsToRequest.size()]), ALL_PERMISSIONS_RESULT); > Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < locationTrack = new LocationTrack(MainActivity.this); if (locationTrack.canGetLocation()) < double longitude = locationTrack.getLongitude(); double latitude = locationTrack.getLatitude(); Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show(); >else < locationTrack.showSettingsAlert(); >> >); > private ArrayList findUnAskedPermissions(ArrayList wanted) < ArrayList result = new ArrayList(); for (String perm : wanted) < if (!hasPermission(perm)) < result.add(perm); >> return result; > private boolean hasPermission(String permission) < if (canMakeSmores()) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < return (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED); >> return true; > private boolean canMakeSmores() < return (Build.VERSION.SDK_INT >Build.VERSION_CODES.LOLLIPOP_MR1); > @TargetApi(Build.VERSION_CODES.M) @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) < switch (requestCode) < case ALL_PERMISSIONS_RESULT: for (String perms : permissionsToRequest) < if (!hasPermission(perms)) < permissionsRejected.add(perms); >> if (permissionsRejected.size() > 0) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < if (shouldShowRequestPermissionRationale(permissionsRejected.get(0))) < showMessageOKCancel("These permissions are mandatory for the application. Please allow access.", new DialogInterface.OnClickListener() < @Override public void onClick(DialogInterface dialog, int which) < if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) < requestPermissions(permissionsRejected.toArray(new String[permissionsRejected.size()]), ALL_PERMISSIONS_RESULT); >> >); return; > > > break; > > private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) < new AlertDialog.Builder(MainActivity.this) .setMessage(message) .setPositiveButton("OK", okListener) .setNegativeButton("Cancel", null) .create() .show(); >@Override protected void onDestroy() < super.onDestroy(); locationTrack.stopListener(); >> 

In the above code, we’re implementing runtime permissions that are used in Android 6.0+ devices. We’ve added the ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the AndroidManifest.xml file. Clicking the button invokes the LocationTrack.java service class. If the location returned is NULL in the case of GPS Provider, we call the showSettingsAlert() method from the LocationTrack.java class that we’ll be seeing shortly. When the activity is destroyed stopLocationTrack() method is called to turn off the location updates. The LocationTrack.java class is defined below.

public class LocationTrack extends Service implements LocationListener < private final Context mContext; boolean checkGPS = false; boolean checkNetwork = false; boolean canGetLocation = false; Location loc; double latitude; double longitude; private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; protected LocationManager locationManager; public LocationTrack(Context mContext) < this.mContext = mContext; getLocation(); >private Location getLocation() < try < locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // get GPS status checkGPS = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // get network provider status checkNetwork = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!checkGPS && !checkNetwork) < Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show(); >else < this.canGetLocation = true; // if GPS Enabled get lat/long using GPS Services if (checkGPS) < if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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. >locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) < loc = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (loc != null) < latitude = loc.getLatitude(); longitude = loc.getLongitude(); >> > /*if (checkNetwork) < if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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. >locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); if (locationManager != null) < loc = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); >if (loc != null) < latitude = loc.getLatitude(); longitude = loc.getLongitude(); >>*/ > > catch (Exception e) < e.printStackTrace(); >return loc; > public double getLongitude() < if (loc != null) < longitude = loc.getLongitude(); >return longitude; > public double getLatitude() < if (loc != null) < latitude = loc.getLatitude(); >return latitude; > public boolean canGetLocation() < return this.canGetLocation; >public void showSettingsAlert() < AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); alertDialog.setTitle("GPS is not Enabled!"); alertDialog.setMessage("Do you want to turn on GPS?"); alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int which) < Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); >>); alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int which) < dialog.cancel(); >>); alertDialog.show(); > public void stopListener() < if (locationManager != null) < if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, 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.removeUpdates(LocationTrack.this); > > @Override public IBinder onBind(Intent intent) < return null; >@Override public void onLocationChanged(Location location) < >@Override public void onStatusChanged(String s, int i, Bundle bundle) < >@Override public void onProviderEnabled(String s) < >@Override public void onProviderDisabled(String s) < >> 

Few inferences drawn from the above code are:

  • In the above code isProviderEnabled(String provider) is called upon the locationManager object and is used to check whether GPS/Network Provider is enabled or not.
  • If the Providers aren’t enabled we’re calling the method showSettingsAlert() that shows a prompt to enable GPS.
  • requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) method of the LocationManager class is used to register the current activity to be notified periodically by the named provider.
  • onLocationChanged is invoked periodically based upon the minTime and minDistance, whichever comes first.
  • Location class hosts the latitude and longitude. To get the current location the following code snippet is used.
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

The output of the above application in action on an emulator is: Our emulators can’t fetch locations, hence it’s returning 0.0 for lat/lng. You can connect your smartphone and run the application in debug mode to check your current location. To emulate GPS Locations in an emulator we can pass fixed Latitude and Longitude values from Android Studio. Besides the emulator window, you can see a list of options. The one at the bottom (which has three dots) is our Extended Controls options. Open that and send a dummy location. Your application should look like this: This brings an end to this tutorial. We’ll be implementing Location API using Google Play Services in a later tutorial. You can download the Android GPSLocationTracking Project from the link below.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Читайте также:  Caused by java lang classnotfoundexception helloworld class
Оцените статью