最近,在看关于经纬度、获取具体位置(地图定位)方面的知识,通过网上的学习以及向大牛们的请教,终于弄清楚地图定位是怎么弄出来的了,哈哈!通过学习,知道了android 定位一般有四种方法,这四种方式分别是:GPS定位,WIFI定准,基站定位,AGPS定位。今天,我要说的是GPS定位,需要GPS硬件支持,直接和卫星交互来获取当前经纬度,这种方式需要手机支持GPS模块(现在大部分的智能机应该都有了)。通过GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电;2,绝大部分用户默认不开启GPS模块;3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间;4,室内几乎无法使用。这其中,缺点2,3都是比较致命的。需要指出的是,GPS走的是卫星通信的通道,在没有网络连接的情况下也能用。因为google自带的获取地址不能用了,所以只能通过接口来解析具体位置,我也是在网上看来的,这个接口是"http://maps.google.cn/maps/api/geocode/json?latlng={0},{1}&sensor=true&language=zh-CN",可以将经纬度提交上去,解析出具体位置,要用的架包有asynhttp网络请求架包、Gson解析Json架包等。
代码如下:
package com.example.mydingwei;
import java.text.MessageFormat;
import org.apache.http.Header;
import android.app.Activity;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import bean.GetAddress;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
public class MainActivity extends Activity {
private TextView tv, jindu, weidu;;
private Location currentLocation;
private LocationManager locationManager;
private String currentProvider;
private String mapUriStr = "http://maps.google.cn/maps/api/geocode/json?latlng={0},{1}&sensor=true&language=zh-CN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
jindu = (TextView) findViewById(R.id.jindu);
weidu = (TextView) findViewById(R.id.weidu);
// 获取到LocationManager对象
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// 创建一个Criteria对象
Criteria criteria = new Criteria();
// 设置粗略精确度
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
// 设置是否需要返回海拔信息
criteria.setAltitudeRequired(false);
// 设置是否需要返回方位信息
criteria.setBearingRequired(false);
// 设置是否允许付费服务
criteria.setCostAllowed(true);
// 设置电量消耗等级
criteria.setPowerRequirement(Criteria.POWER_HIGH);
// 设置是否需要返回速度信息
criteria.setSpeedRequired(false);
// 根据设置的Criteria对象,获取最符合此标准的provider对象 41
currentProvider = locationManager.getBestProvider(criteria, true);
Log.d("Location", "currentProvider: " + currentProvider);
// 根据当前provider对象获取最后一次位置信息 44
currentLocation = locationManager.getLastKnownLocation(currentProvider);
// 如果位置信息为null,则请求更新位置信息 46
if (currentLocation == null) {
locationManager.requestLocationUpdates(currentProvider, 0, 0,
locationListener);
}
// 直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度 50
// 每隔8秒获取一次位置信息 51
MyThread myThread = new MyThread();
myThread.start();
getOk();
}
/**
*
* @Method: getOk
* @Description: asynhttp请求方式
* @param:
* @return: void
* @author: liweikang
*/
private void getOk() {
String uriStr = MessageFormat.format(mapUriStr,
currentLocation.getLatitude(), currentLocation.getLongitude());
AsyncHttpClient client = new AsyncHttpClient();
client.get(uriStr, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
String responseBody) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, responseBody);
Gson gson = new Gson();
GetAddress ga = gson.fromJson(responseBody, GetAddress.class);
if (ga.getStatus().equals("OK")) {
bean.Address address = ga.getResults().get(1);
tv.setText(address.getFormatted_address());
} else {
Toast.makeText(MainActivity.this, "地址获取失败!",
Toast.LENGTH_SHORT).show();
}
}
});
}
// 创建位置监听器 85
private LocationListener locationListener = new LocationListener() {
// 位置发生改变时调用 87
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
Log.d("Location",
"onLocationChanged Latitude" + location.getLatitude());
Log.d("Location",
"onLocationChanged location" + location.getLongitude());
}
// provider失效时调用 95
@Override
public void onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
}
// provider启用时调用101
@Override
public void onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
}
// 状态改变时调用107
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
};
/**
*
* @ClassName: MyThread
* @Description:得到本地的经纬度
* @author: liweikang
* @date: 2016-10-8 下午1:44:38
*/
private class MyThread extends Thread {
@Override
public void run() {
while (true) {
super.run();
currentLocation = locationManager
.getLastKnownLocation(currentProvider);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (currentLocation != null) {
Log.d("Location",
"Latitude: "
+ currentLocation.getLatitude());
Log.d("Location",
"location: "
+ currentLocation.getLongitude());
jindu.setText("经度:\n"
+ currentLocation.getLongitude());
weidu.setText("纬度:\n"
+ currentLocation.getLatitude());
} else {
Log.d("Location", "Latitude: " + 0);
Log.d("Location", "location: " + 0);
jindu.setText("经度:0");
weidu.setText("纬度:0");
}
}
});
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
Log.e("Location", e.getMessage());
}
}
}
}
}
xml如下:
<ScrollView 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: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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/jindu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@android:color/holo_blue_bright"/>
<TextView
android:id="@+id/weidu"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="@android:color/holo_blue_bright"/>
</LinearLayout>
<TextView
android:id="@+id/tv"
android:layout_below="@id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="正在搜索位置..."/>
</LinearLayout>
</ScrollView>
还有权限别忘记配了哦,
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>