在Android应用程序中,能够使用LocationManager来获取移动设备所在的地理位置信息。看以下实例:新建android应用程序TestLocation。html
一、activity_main.xml布局文件java
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
-
- <TextView
- android:id="@+id/positionView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
-
- </LinearLayout>
用于显示获取到的位置信息。android
二、MainActivity.javagit
从上面能够看出,获取地理位置信息主要分以下步骤:web
(1)获取LocationManager实例,经过getSystemService方法,传入Context.LOCATION_SERVICE参数。apache
(2)获取可用的位置提供器,有GPS_PROVIDER、NETWORK_PROVIDER、PASSIVE_PROVIDER三种,前两种比较经常使用。json
(3)将(2)获取到的位置提供器传入LocationManager的方法getLastKnownLocation,便可获取Location信息。api
若是移动设备地理位置不断发生变化,则实时更新须要进行以下步骤:app
(4)调用LocationManager的requestLocationUpdates方法,第一个参数是位置提供器,第二个参数是监听位置变化的时间间隔(毫秒),第三个参数是监听位置变化的距 离间隔(米),第四个参数是LocationListener监听器ide
(5)当位置发生变化后,就会调用监听器的onLocationChanged方法。
(6)为了省电,节约资源,当程序关闭后,调用LocationManager的removeUpdates方法移除监听器。
三、获取权限
修改AndroidManifest.xml,添加以下代码:
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
-
四、效果
使用模拟器进行测试:点击send

能够使用Geocoding API查找具体对应的位置。以下:
(1)修改MainActivity.java
- package com.example.testlocation;
-
- 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 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.view.Menu;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
-
- private TextView postionView;
-
- private LocationManager locationManager;
- private String locationProvider;
-
- public static final int SHOW_LOCATION = 0;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
- postionView = (TextView) findViewById(R.id.positionView);
-
- locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
-
- List<String> providers = locationManager.getProviders(true);
- if(providers.contains(LocationManager.GPS_PROVIDER)){
-
- locationProvider = LocationManager.GPS_PROVIDER;
- }else if(providers.contains(LocationManager.NETWORK_PROVIDER)){
-
- locationProvider = LocationManager.NETWORK_PROVIDER;
- }else{
- Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
- return ;
- }
-
- Location location = locationManager.getLastKnownLocation(locationProvider);
-
- if(location!=null){
-
-
- showLocation(location);
- }else{
- Toast.makeText(this, "location为空", Toast.LENGTH_SHORT).show();
- }
-
- locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);
-
- }
-
- private Handler handler = new Handler(){
- public void handleMessage(Message msg){
- switch(msg.what){
- case SHOW_LOCATION:
- String position = (String) msg.obj;
- postionView.setText(position);
- break;
- default:
- break;
- }
- }
- };
-
- private void showLocation(final Location location){
-
- new Thread(new Runnable() {
-
- @Override
- public void run() {
- try{
-
- StringBuilder url = new StringBuilder();
- url.append("http://maps.googleapis.com/maps/api/geocode/json?latlng=");
- url.append(location.getLatitude()).append(",");
- url.append(location.getLongitude());
- url.append("&sensor=false");
- HttpClient client = new DefaultHttpClient();
- HttpGet httpGet = new HttpGet(url.toString());
- httpGet.addHeader("Accept-Language","zh-CN");
- HttpResponse response = client.execute(httpGet);
- if(response.getStatusLine().getStatusCode() == 200){
- HttpEntity entity = response.getEntity();
- String res = EntityUtils.toString(entity);
-
- JSONObject jsonObject = new JSONObject(res);
-
- 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();
- }
-
-
-
- LocationListener locationListener = new LocationListener() {
-
- @Override
- public void onStatusChanged(String provider, int status, Bundle arg2) {
-
- }
-
- @Override
- public void onProviderEnabled(String provider) {
-
- }
-
- @Override
- public void onProviderDisabled(String provider) {
-
- }
-
- @Override
- public void onLocationChanged(Location location) {
-
- showLocation(location);
-
- }
- };
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(locationManager!=null){
-
- locationManager.removeUpdates(locationListener);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }
(2)修改AndroidManifest.xml
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.INTERNET"/>