android的GPS信息是通过LocationManager来获取的;
下面是一个获取GPS信息的各种消息的类子:
package com.example.gpscommit;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener{
protected static final int SHOW_LOCATION = 0;
private LocationManager locationManager;
private String locationProvider;
//findviewbyid 注解
@InjectView(R.id.tv0)
TextView tv0;
@InjectView(R.id.tv1)
TextView tv1;
@InjectView(R.id.tv2)
TextView tv2;
@ViewInject(R.id.tv3)
private TextView tv3;
@ViewInject(R.id.tv4)
private TextView tv4;
@ViewInject(R.id.tv5)
private TextView tv5;
@ViewInject(R.id.tv6)
private TextView tv6;
@ViewInject(R.id.tv7)
private TextView tv7;
@ViewInject(R.id.tv8)
private TextView tv8;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//findviewbyid 注解
ViewUtils.inject(this);
ButterKnife.inject(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
List<String> providers = locationManager.getProviders(true);
if(providers.contains(LocationManager.GPS_PROVIDER)){
//如果是GPS
locationProvider = LocationManager.GPS_PROVIDER;
}else if(providers.contains(LocationManager.NETWORK_PROVIDER)){
//如果是Network
locationProvider = LocationManager.NETWORK_PROVIDER;
}else{
Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
return ;
}
//获取Location
Location location = locationManager.getLastKnownLocation(locationProvider);
if(location!=null){
showLocation(location);
getLocationContent(location);
}
//监视地理位置变化
locationManager.requestLocationUpdates(locationProvider, 1000, 0, this);
}
public void onLocationChanged(Location location) {
showLocation(location);
getLocationContent(location);
}
private void getLocationContent(Location location) {
tv1.setText(location.toString());
float accuracy = location.getAccuracy();//得到精度,in meters
tv2.setText("精度"+accuracy);
double altitude = location.getAltitude();//得到海拔高度,in meters
tv3.setText("海拔"+altitude);
float bearing = location.getBearing();//得到度数(0.0, 360.0]
tv4.setText("度数"+bearing);
double latitude = location.getLatitude();//得到纬度,in degrees
tv5.setText("纬度"+latitude);
double longitude = location.getLongitude();//得到经度,in degrees
tv6.setText("经度"+longitude);
float speed = location.getSpeed();//得到运行速度,in meters/second
tv7.setText("速度"+speed);
long time = location.getTime();//in milliseconds since January 1, 1970
tv8.setText("时间"+time);
System.out.println("----------------"+location.toString());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private Handler handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case SHOW_LOCATION:
String position = (String) msg.obj;
tv0.setText(position);
break;
default:
break;
}
}
};
/**
* 显示地理位置经度和纬度信息
* @param location
*/
private void showLocation(final Location location){
new Thread(new Runnable() {
@Override
public void run() {
try{
//百度地理编码服务
//http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderOption
//&output=json&address=百度大厦&city=北京市
//百度逆地理编码服务,返回结果为json型
//http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse
//&location=39.983424,116.322987&output=json&pois=1
//百度逆地理编码服务,返回结果为xml型
//http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderReverse
//&location=39.983424,116.322987&output=xml&pois=1
//组装反向地理编码的接口位置
StringBuilder url = new StringBuilder();
url.append("http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045"
+ "&callback=renderReverse&location=");
url.append(location.getLatitude()).append(",");
url.append(location.getLongitude());
url.append("&output=json&pois=1");
HttpGet httpGet = new HttpGet(url.toString());
httpGet.addHeader("Accept-Language","zh-CN");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
if(response.getStatusLine().getStatusCode() == 200){
HttpEntity entity = response.getEntity();
String res = EntityUtils.toString(entity);
//解析
JSONObject jsonObject = new JSONObject(res);
//获取results节点下的位置信息
JSONArray resultArray = jsonObject.getJSONArray("results");
if(resultArray.length() > 0){
JSONObject obj = resultArray.getJSONObject(0);
//取出格式化后的位置数据
String address = obj.getString("formatted_address");
Message msg = new Message();
msg.what = SHOW_LOCATION;
msg.obj = address;
handler.sendMessage(msg);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
}
}
xml文件:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.gpscommit.MainActivity" >
<TextView
android:id="@+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>