preloader

Search on AEQ-WEB

AEQ-WEB | Blog

Android GPS Data & Position Java

Android Read GPS

5 Jahre AEQ-WEB - Zeig dein Projekt

On this page, we will show you how to access the GPS API in android and how you can work with the parameters. The app checks if the permissions for the location services are given. If it is activated, then the app starts to locate. When the refresh button is pressed, "onLocationChanged"-function is called. This function is also called automatically when Android notices that the position changes. The app also tries to determine the name of the locality where you are currently located.

Determined GPS parameters

  • Latitude
  • Longitude
  • Locality
  • Speed
  • Altitude
  • Satellites

GPS_Info.apk
Download
GPS_Info Source
Download

First, the authorization for GPS access must be defined in the manifest:

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

Then copy the code to MainActivity:


package aeqweb.com.gpsinfo;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
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.v4.content.ContextCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
        implements OnClickListener {

    private LocationManager locationMangaer = null;
    private LocationListener locationListener = null;
    private Button btnGetLocation = null;
    private TextView v_longitude = null;
    private TextView v_latitude = null;
    private TextView v_location = null;
    private TextView v_speed = null;
    private TextView v_altitude = null;
    private TextView v_sat = null;
    private TextView v_gps_status = null;
    private TextView v_update_status = null;
    String longitude = "";
    String latitude = "";
    String location = "";
    String speed = "";
    String altitude = "";
    String sat_nr = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //portrait mode
        v_longitude = (TextView) findViewById(R.id.Lon_view);
        v_latitude = (TextView) findViewById(R.id.Lat_view);
        v_location = (TextView) findViewById(R.id.Loc_view);
        v_speed = (TextView) findViewById(R.id.Spd_view);
        v_altitude = (TextView) findViewById(R.id.Alt_view);
        v_sat = (TextView) findViewById(R.id.Sat_view);
        v_gps_status = (TextView) findViewById(R.id.Gps_status);
        v_update_status = (TextView) findViewById(R.id.Update_status);

        btnGetLocation = (Button) findViewById(R.id.button);
        btnGetLocation.setOnClickListener(this);
        locationMangaer = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public void onClick(View v) {
        //Check if GPS is enabled
        locationListener = new APPLocationListener();
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
                PackageManager.PERMISSION_GRANTED) {
            v_gps_status.setText("Wait for signal");
            v_gps_status.setTextColor(Color.parseColor("#0066ff"));
            locationMangaer.requestLocationUpdates(LocationManager
                    .GPS_PROVIDER, 5000, 10, locationListener);
        } else {
            v_gps_status.setText("No GPS-Access!!!");
            v_gps_status.setTextColor(Color.parseColor("#ff0000"));
        }
    }

    private class APPLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            //Try to get city name
            String city_name = null;
            Geocoder gcd = new Geocoder(getBaseContext(),
                    Locale.getDefault());
            List
addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(), loc .getLongitude(), 1); if (addresses.size() > 0) city_name = addresses.get(0).getLocality(); } catch (IOException e) { city_name = "unknown"; e.printStackTrace(); } latitude = "" + loc.getLatitude(); //Get latitude longitude = "" + loc.getLongitude(); //Get longitude location = "" + city_name; //Get city name speed = "" + loc.getSpeed() + " m/s"; //Get speed in meters per second altitude = "" + loc.getAltitude() + " m"; //Get height in meters sat_nr = "" + loc.getExtras().getInt("satellites"); //available satellites v_latitude.setText(latitude); v_longitude.setText(longitude); v_location.setText(location); v_speed.setText(speed); v_altitude.setText(altitude); v_sat.setText(sat_nr); v_gps_status.setText("GPS working"); v_gps_status.setTextColor(Color.parseColor("#33cc33")); Calendar c = Calendar.getInstance(); //Get time on system v_update_status.setText("Last update: " + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } } }


Info: This page was automatically translated and may contain errors
Share:
thumbnail
05.06.2018
Android Image Upload

On this page we show you how to send an image via an Android APP with HTTP-POST to a PHP server. The source code is available here.

Alex @ AEQ-WEB
thumbnail
05.06.2018
Android Image Download

On this page we show you how to download an image with Android.The app download the image with an asynchronous task from a PHP-File on a webserver

Alex @ AEQ-WEB