Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AndroidManifest.xml
- ==============================
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.zeevm.mygps">
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- MainActivity.java
- ==================
- package com.example.zeevm.mygps;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements LocationListener {
- TextView txtGPS;
- Location myLocation;
- Context context;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- runMain();
- }
- private void runMain()
- {
- myLocation=getGPS();
- String str="";
- str+="Long :"+myLocation.getLongitude();
- str+="Lat :"+myLocation.getLatitude();
- str+="Alt :"+myLocation.getAltitude();
- str+="Provider:"+myLocation.getProvider();
- str+="Bearing :"+myLocation.getBearing();
- str+="Speed :"+myLocation.getSpeed();
- str+="Time :"+myLocation.getTime();
- str+="Accuracy:"+myLocation.getAccuracy();
- Location myLocation2=null;
- //distance between two points.
- double myDistance = myLocation.distanceTo(myLocation2);
- }
- private void setPointer() {
- this.context = this;
- txtGPS = (TextView) findViewById(R.id.txtGPS);
- }
- private Location getGPS() {
- LocationManager manger = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- Toast.makeText(context, "There is no permission to GPS", Toast.LENGTH_SHORT).show();
- return null;
- }
- Location locationGPS = manger.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- Location locationNET = manger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- long GPSlocationTime = locationGPS != null ? locationGPS.getTime() : 0;
- long NetLocationTime = locationNET != null ? locationNET.getTime() : 0;
- return (GPSlocationTime > NetLocationTime)?locationGPS:locationNET;
- }
- @Override
- public void onLocationChanged(Location location) {
- myLocation = location;
- }
- @Override
- public void onStatusChanged(String s, int i, Bundle bundle) {
- //
- }
- @Override
- public void onProviderEnabled(String s) {
- Toast.makeText(this, s + " is enabled", Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onProviderDisabled(String s) {
- Toast.makeText(this, s + " is disabled", Toast.LENGTH_SHORT).show();
- }
- }
- activity_main.xml
- =========================
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/txtGPS"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="GPS DATA" />
- </LinearLayout>
Add Comment
Please, Sign In to add comment