MainActivity.java
package com.getcurrentlonglat; import android.annotation.SuppressLint; import android.content.Intent; import android.location.Address; import android.location.Geocoder; import android.net.Uri; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; 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.TextView; import java.io.IOException; import java.util.List; import java.util.Locale; public class Main2Activity extends AppCompatActivity implements View.OnClickListener { private static final int REQUEST_CODE_PERMISSION = 2; List<Address> addresses; Button btnShowLocation,buttonshow; Geocoder geocoder; GPSTracker gps; String mPermission = "android.permission.ACCESS_FINE_LOCATION"; TextView txt_add; TextView txt_lat; TextView txt_long; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); txt_lat = (TextView) findViewById(R.id.txt_lat); txt_long = (TextView) findViewById(R.id.txt_long); txt_add = (TextView) findViewById(R.id.txt_add); try { if (ContextCompat.checkSelfPermission(this, this.mPermission) != 0) { ActivityCompat.requestPermissions(this, new String[]{this.mPermission}, 2); } } catch (Exception e) { e.printStackTrace(); } btnShowLocation = (Button) findViewById(R.id.button); buttonshow = (Button)findViewById(R.id.buttonshow); buttonshow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { gps = new GPSTracker(Main2Activity.this); if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); try { getCompleteAddressString(latitude, longitude); } catch (IOException e) { e.printStackTrace(); } final String lat = String.valueOf(latitude); final String lang = String.valueOf(longitude); txt_lat.setText(lat); txt_long.setText(lang); String tempid="google.navigation:q="+"23.0734262,72.6243823"; //String tempid="google.navigation?q="+lat+","+lang; Uri gmmIntentUri = Uri.parse(tempid); Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); if (mapIntent.resolveActivity(getPackageManager()) != null) { startActivity(mapIntent); } return; } gps.showSettingsAlert(); /* gps = new GPSTracker(Main2Activity.this); if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); try { getCompleteAddressString(latitude, longitude); } catch (IOException e) { e.printStackTrace(); } final String lat = String.valueOf(latitude); final String lang = String.valueOf(longitude); txt_lat.setText(lat); txt_long.setText(lang); String tempid="geo:0,0?q="+lat+","+lang; //String tempid="google.navigation?q="+lat+","+lang; Uri gmmIntentUri = Uri.parse(tempid); Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); mapIntent.setPackage("com.google.android.apps.maps"); if (mapIntent.resolveActivity(getPackageManager()) != null) { startActivity(mapIntent); } return; } gps.showSettingsAlert();*/ } }); btnShowLocation.setOnClickListener(this); } @Override public void onClick(View view) { gps = new GPSTracker(Main2Activity.this); if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); try { getCompleteAddressString(latitude, longitude); } catch (IOException e) { e.printStackTrace(); } final String lat = String.valueOf(latitude); final String lang = String.valueOf(longitude); txt_lat.setText(lat); txt_long.setText(lang); return; } gps.showSettingsAlert(); } @SuppressLint("LongLogTag") private String getCompleteAddressString(double LATITUDE, double LONGITUDE) throws IOException { String strAdd = ""; try { List<Address> addresses = new Geocoder(this, Locale.getDefault()).getFromLocation(LATITUDE, LONGITUDE, 1); if (addresses != null) { Address returnedAddress = (Address) addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder(""); for (int i = 0; i <= returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); } strAdd = strReturnedAddress.toString(); txt_add.setText(strAdd); Log.w("My Current loction address", strReturnedAddress.toString()); return strAdd; } Log.w("My Current loction address", "No Address returned!"); return strAdd; } catch (Exception e) { e.printStackTrace(); Log.w("My Current loction address", "Canont get Address!"); return strAdd; } } }
GPSTracker.java
package com.getcurrentlonglat; import android.app.AlertDialog.Builder; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.util.Log; public class GPSTracker extends Service implements LocationListener { private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; private static final long MIN_TIME_BW_UPDATES = 60000; boolean canGetLocation = false; boolean isGPSEnabled = false; boolean isNetworkEnabled = false; double latitude; Location location; protected LocationManager locationManager; double longitude; private final Context mContext; class C01931 implements OnClickListener { C01931() { } public void onClick(DialogInterface dialog, int which) { GPSTracker.this.mContext.startActivity(new Intent("android.settings.LOCATION_SOURCE_SETTINGS")); } } class C01942 implements OnClickListener { C01942() { } public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } } public GPSTracker(Context context) { this.mContext = context; getLocation(); } public Location getLocation() { try { this.locationManager = (LocationManager) this.mContext.getSystemService("location"); this.isGPSEnabled = this.locationManager.isProviderEnabled("gps"); this.isNetworkEnabled = this.locationManager.isProviderEnabled("network"); if (this.isGPSEnabled || this.isNetworkEnabled) { this.canGetLocation = true; if (this.isNetworkEnabled) { this.locationManager.requestLocationUpdates("network", MIN_TIME_BW_UPDATES, 10.0f, this); Log.d("Network", "Network"); if (this.locationManager != null) { this.location = this.locationManager.getLastKnownLocation("network"); if (this.location != null) { this.latitude = this.location.getLatitude(); this.longitude = this.location.getLongitude(); } } } if (this.isGPSEnabled && this.location == null) { this.locationManager.requestLocationUpdates("gps", MIN_TIME_BW_UPDATES, 10.0f, this); Log.d("GPS Enabled", "GPS Enabled"); if (this.locationManager != null) { this.location = this.locationManager.getLastKnownLocation("gps"); if (this.location != null) { this.latitude = this.location.getLatitude(); this.longitude = this.location.getLongitude(); } } } } } catch (Exception e) { e.printStackTrace(); } return this.location; } public void stopUsingGPS() { if (this.locationManager != null) { this.locationManager.removeUpdates(this); } } public double getLatitude() { if (this.location != null) { this.latitude = this.location.getLatitude(); } return this.latitude; } public double getLongitude() { if (this.location != null) { this.longitude = this.location.getLongitude(); } return this.longitude; } public boolean canGetLocation() { return this.canGetLocation; } public void showSettingsAlert() { Builder alertDialog = new Builder(this.mContext); alertDialog.setTitle("GPS is settings"); alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); alertDialog.setPositiveButton("Settings", new C01931()); alertDialog.setNegativeButton("Cancel", new C01942()); alertDialog.show(); } public void onLocationChanged(Location location) { } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { } public IBinder onBind(Intent arg0) { return null; } }activity_main.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" app:layout_width="match_parent" app:layout_height="match_parent" app:layout_margin="10dp" tools:ignore="NamespaceTypo"> <LinearLayout app:orientation="vertical" app:id="@+id/linearLayout" app:layout_width="match_parent" app:layout_height="wrap_content"> <TextView app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:text="Latitude" /> <TextView app:id="@+id/txt_lat" app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:hint="Lat" /> </LinearLayout> <LinearLayout app:orientation="vertical" app:id="@+id/linearLayout2" app:layout_width="match_parent" app:layout_height="wrap_content" app:layout_below="@+id/linearLayout" app:layout_alignParentLeft="true"> <TextView app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:text="Longitude" /> <TextView app:id="@+id/txt_long" app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:hint="Long" /> </LinearLayout> <LinearLayout app:orientation="vertical" app:layout_width="match_parent" app:layout_height="wrap_content" app:layout_below="@+id/linearLayout2" app:layout_alignParentLeft="true"> <TextView app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:text="Address" /> <TextView app:id="@+id/txt_add" app:padding="5dp" app:layout_width="match_parent" app:layout_height="wrap_content" app:hint="Address" /> </LinearLayout> <Button app:id="@+id/button" app:layout_width="match_parent" app:layout_height="wrap_content" app:layout_alignParentBottom="true" app:layout_alignParentLeft="true" app:layout_alignParentStart="true" app:text="get location" /> <Button app:id="@+id/buttonshow" app:layout_width="match_parent" app:layout_height="wrap_content" app:layout_above="@+id/button" app:layout_alignParentLeft="true" app:layout_alignParentStart="true" app:text="Show location" /> </RelativeLayout>
No comments:
Post a Comment