Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active January 26, 2016 08:12
Show Gist options
  • Save daichan4649/6068312 to your computer and use it in GitHub Desktop.
Save daichan4649/6068312 to your computer and use it in GitHub Desktop.
get GPS information (Android)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/provider"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dip"
android:text="GMS" />
<TextView
android:id="@+id/gms_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/gms_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="daichan4649.gps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="daichan4649.gps.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package daichan4649.gps;
public class GpsUtil {
// http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
public static String convertDecimal2DMS(double coord) {
String output, degrees, minutes, seconds;
// gets the modulus the coordinate divided by one (MOD1).
// in other words gets all the numbers after the decimal point.
// e.g. mod = 87.728056 % 1 == 0.728056
//
// next get the integer part of the coord. On other words the whole number part.
// e.g. intPart = 87
double mod = coord % 1;
int intPart = (int) coord;
//set degrees to the value of intPart
//e.g. degrees = "87"
degrees = String.valueOf(intPart);
// next times the MOD1 of degrees by 60 so we can find the integer part for minutes.
// get the MOD1 of the new coord to find the numbers after the decimal point.
// e.g. coord = 0.728056 * 60 == 43.68336
// mod = 43.68336 % 1 == 0.68336
//
// next get the value of the integer part of the coord.
// e.g. intPart = 43
coord = mod * 60;
mod = coord % 1;
intPart = (int) coord;
// set minutes to the value of intPart.
// e.g. minutes = "43"
minutes = String.valueOf(intPart);
//do the same again for minutes
//e.g. coord = 0.68336 * 60 == 41.0016
//e.g. intPart = 41
coord = mod * 60;
intPart = (int) coord;
// set seconds to the value of intPart.
// e.g. seconds = "41"
seconds = String.valueOf(intPart);
// I used this format for android but you can change it
// to return in whatever format you like
// e.g. output = "87/1,43/1,41/1"
output = degrees + "/1," + minutes + "/1," + seconds + "/1";
//Standard output of D°M′S″
//output = degrees + "°" + minutes + "'" + seconds + "\"";
return output;
}
public static double convertDMS2Decimal(String hemisphereOUmeridien, double degres, double minutes, double secondes) {
double LatOrLon = 0;
double signe = 1.0;
if ((hemisphereOUmeridien.equals("W")) || (hemisphereOUmeridien.equals("S"))) {
signe = -1.0;
}
LatOrLon = signe * (Math.floor(degres) + Math.floor(minutes) / 60.0 + secondes / 3600.0);
return (LatOrLon);
}
public static String getLatitudeRef(double latitude) {
return latitude < 0.0d ? "S" : "N";
}
public static String getLongitudeRef(double longitude) {
return longitude < 0.0d ? "W" : "E";
}
}
package daichan4649.gps;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends Activity {
private LocationManager mLocationManager = null;
private TextView mProvider;
private TextView mLatitude;
private TextView mLongitude;
private TextView mGmsLatitude;
private TextView mGmsLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mProvider = (TextView) findViewById(R.id.provider);
mLatitude = (TextView) findViewById(R.id.latitude);
mLongitude = (TextView) findViewById(R.id.longitude);
mGmsLatitude = (TextView) findViewById(R.id.gms_latitude);
mGmsLongitude = (TextView) findViewById(R.id.gms_longitude);
}
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
// decimal
double latitude = location.getLatitude();
double longitude = location.getLongitude();
mLatitude.setText(Double.toString(latitude));
mLongitude.setText(Double.toString(longitude));
// dms
String dmsLatitude = GpsUtil.convertDecimal2DMS(latitude);
String refLatitude = GpsUtil.getLatitudeRef(latitude);
String dmsLongitude = GpsUtil.convertDecimal2DMS(longitude);
String refLongitude = GpsUtil.getLongitudeRef(longitude);
mGmsLatitude.setText(String.format("lat=%s ref=%s", dmsLatitude, refLatitude));
mGmsLongitude.setText(String.format("long=%s ref=%s", dmsLongitude, refLongitude));
}
};
@Override
protected void onResume() {
super.onResume();
// GPS更新
updateGpsInfo();
}
@Override
protected void onPause() {
super.onPause();
mLocationManager.removeUpdates(mLocationListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.update:
updateGpsInfo();
break;
}
return super.onOptionsItemSelected(item);
}
private void updateGpsInfo() {
String provider = getBestProvider();
mLocationManager.requestLocationUpdates(provider, 0, 0, mLocationListener);
mProvider.setText(provider);
mLatitude.setText("update...");
mLongitude.setText("update...");
mGmsLatitude.setText("");
mGmsLongitude.setText("");
}
private String getBestProvider() {
Criteria criteria = new Criteria();
// 精度
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// 消費電力
criteria.setPowerRequirement(Criteria.POWER_LOW);
return mLocationManager.getBestProvider(criteria, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment