Android实现自动通过GPS定位用户所在地名
来源:互联网 发布:手机淘宝换回默认皮肤 编辑:程序博客网 时间:2024/05/17 22:52
最近开始跟随实验室做Android项目,因为之前一直在写PHP,所以比较吃力,这里写的功能借鉴了CSDN博客的大神的博文,在这里提出谢谢了(大神博文的地址: http://blog.csdn.net/octobershiner/article/details/6628370)。
本来就是想做一个定位获取到经纬度的,老大说要把地名获取放到客户端,只能咬牙做了。
这里只上传一个功能类:
package com.example.helloworld;import java.io.IOException;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONException;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.util.Log;/** *提供设备获取位置信息的入口,通过GPS进行定位 *需要请求定位权限(android.permission.ACCESS_FINE_LOCATION) *实例化时必须传参,将Activity传进来,不然会报错 */public class GetLocationInfo {private LocationManager manager;private Activity activity;private static final String TAG = "LOCATION DEMO";public GetLocationInfo(Activity activity) {this.activity = activity;}public String getLocInfo() {String locInfo = null;//获取系统服务manager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 10, locationListener); //获取位置信息locInfo = updateLocation(location);return locInfo;} private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); } public void onProviderDisabled(String provider){ updateLocation(null); Log.i(TAG, "Provider now is disabled.."); } public void onProviderEnabled(String provider){ Log.i(TAG, "Provider now is enabled.."); } public void onStatusChanged(String provider, int status,Bundle extras){ } }; private String updateLocation(Location location) {String latLng = null;if(location != null) {final double lat = location.getLatitude();final double lng = location.getLongitude();new Thread(){public void run() {getLocName(lat, lng);};}.start();//latLng = getLocName(lat, lng);//latLng = "Latitude:" + lat + " Longitude:" + lng;} else {latLng = "Can't access your location";}return latLng;}private String getLocName(double latitude, double longitude) {String locName = null;String key = "OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77";String uri = "http://apis.map.qq.com/ws/geocoder/v1/?location=" + latitude + ',' + longitude + "&key=" + key;/*建立HTTP GET 对象*/HttpGet httpRequest = new HttpGet(uri);try {/*发送请求并等待响应*/ HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); /*若状态码为200 ok*/ if(httpResponse.getStatusLine().getStatusCode() == 200) { /*读*/ String strResult = EntityUtils.toString(httpResponse.getEntity()); locName = changeJsonToArray(strResult);Log.d("message", locName);} else { locName = "反地址解析请求中断";} } catch(ClientProtocolException e) {locName = e.getMessage().toString(); e.printStackTrace(); } catch(IOException e) {locName = e.getMessage().toString();e.printStackTrace();} catch(Exception e) {locName = e.getMessage().toString();e.printStackTrace();}return locName;}private static String changeJsonToArray(String json){String jsonArray = null;JSONObject jsonObject = null;try {jsonObject = new JSONObject(json);jsonArray = jsonObject.getString("result");jsonObject = new JSONObject(jsonArray);jsonArray = jsonObject.getString("address");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();} return jsonArray;}}
0 0
- Android实现自动通过GPS定位用户所在地名
- Android GPS定位实现
- Android 通过GPS进行定位
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android实现GPS定位功能
- Android GPS 定位的实现
- android用户定位GPS开发(一)
- Android GPS定位 一分钟快速实现GPS定位
- GPS用户定位
- unity通过GPS定位
- Android编程之实现GPS定位
- Android编程之实现GPS定位
- Android下实现GPS定位服务
- Android编程之实现GPS定位
- Android 基于googleAPI 实现gps定位
- 反向互补子串
- ThinkPHP许愿池 笔记
- FileItem类的常用方法
- 菜鸟第一次写shell脚本:加法运算
- codeforces 427 D Match & Catch ( 后缀数组 )
- Android实现自动通过GPS定位用户所在地名
- fedora20卸载maxthon
- java中二分查找的总结
- android.os.NetworkOnMainThreadException
- 自定义ViewGroup实现垂直滚动
- Oracle 表空间与数据文件
- Yii Criteria常用方法
- 算法竞赛入门经典(第2版)习题3-11 换低挡装置 Kickdown UVa1588
- JQuery中$.ajax()方法参数详解