android 经纬度 谷歌,android:GPS获取location经纬度并用谷歌解析为地理位置名称
实现的功能:先获取本地的经纬度,再根据经纬度,请求googleapis来解析地理位置名称。
下面的例子,能够跑起来,亲测。
多说无益,看码。
首先搞一个布局,其实就是一个textView,一个button,点击button后,在textview展示地理位置信息。
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" >
android:id="@+id/tv_location_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/btn_show_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取当前位置" >
为了方便,我直接把获取地理位置、经纬度解析,都放到MainActivity里面了。(其实就是懒,这样不好,还是该做什么的类去做什么,应该分开放的)
package com.example.getlocation;
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.JSONException;
import org.json.JSONObject;
import android.Manifest;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private LocationManager locationManager;// 位置服务
private Location location;// 位置
private Button btn_show;// 按钮
private TextView tv_show;// 展示地理位置
public static final int SHOW_LOCATION = 0;
private String lastKnowLoc;
//监听地理位置变化,地理位置变化时,能够重置location
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
tv_show.setText("更新失败失败");
}
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
location = loc;
showLocation(location);
}
}
};
//这个不用说了,主要是初始化页面控件,并且设置按钮监听
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();// 关联控件,初始化按钮、展示框
tv_show.setText("地理位置");// 设置默认的位置展示,也就是没有点击按钮时的展示
lastKnowLoc = "中国";
// 点击按钮时去初始化一次当前位置
btn_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
locationInit();// 调用位置初始化方法
}
});
}
//这个就是地理位置初始化,主要通过调用其他方法获取经纬度,并设置到location
public void locationInit() {
try {
// 获取系统服务
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// 判断GPS是否可以获取地理位置,如果可以,展示位置,
// 如果GPS不行,再判断network,还是获取不到,那就报错
if (locationInitByGPS() || locationInitByNETWORK()) {
// 上面两个只是获取经纬度的,获取经纬度location后,再去调用谷歌解析来获取地理位置名称
showLocation(location);
} else {
tv_show.setText("获取地理位置失败,上次的地理位置为:" + lastKnowLoc);
}
} catch (Exception e) {
tv_show.setText("获取地理位置失败,上次的地理位置为:" + lastKnowLoc);
}
}
// GPS去获取location经纬度
public boolean locationInitByGPS() {
// 没有GPS,直接返回
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
return false;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
return true;//设置location成功,返回true
} else {
return false;
}
}
// network去获取location经纬度
public boolean locationInitByNETWORK() {
// 没有NETWORK,直接返回
if (!locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
return false;
}
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
return true;
} else {
return false;
}
}
//控件初始化
private void init() {
btn_show = (Button) findViewById(R.id.btn_show_location);
tv_show = (TextView) findViewById(R.id.tv_location_show);
}
//这是根据经纬度,向谷歌的API解析发网络请求,然后获取response,这里超时时间不要太短,否则来不及返回位置信息,直接失败了
private void showLocation(final Location loc) {
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(loc.getLatitude()).append(",");
url.append(loc.getLongitude());
url.append("&sensor=false");
// 通过HttpClient去执行HttpGet
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
httpGet.addHeader("Accept-Language", "zh-CN");
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
JSONObject jsonObject = new JSONObject(response);
JSONArray resultArray = jsonObject
.getJSONArray("results");
if (resultArray.length() > 0) {
JSONObject subObject = resultArray.getJSONObject(0);
String address = subObject
.getString("formatted_address");
Message message = new Message();
message.what = SHOW_LOCATION;
message.obj = address;
lastKnowLoc = address;
handler.sendMessage(message);
}
} else {
Message message = new Message();
message.what = SHOW_LOCATION;
message.obj = "获取地理位置失败,上次的地理位置为:" + lastKnowLoc;
handler.sendMessage(message);
}
} catch (Exception e) {
Message message = new Message();
message.what = SHOW_LOCATION;
message.obj = "获取地理位置失败,上次的地理位置为:" + lastKnowLoc;
handler.sendMessage(message);
e.printStackTrace();
}
}
}).start();
}
//就是展示下获取到的地理位置
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_LOCATION:
String currentPosition = (String) msg.obj;
tv_show.setText(currentPosition);
break;
default:
break;
}
}
};
}
上面基本就做好了,但是还不行,因为获取用户地理位置这样的LBS获取,需要申请权限,否则不允许的,程序会报错。
android 经纬度 谷歌,android:GPS获取location经纬度并用谷歌解析为地理位置名称相关推荐
- 【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取定位信息 开启定位相关功能只是将定位的前提条件准备好,若想获得手机当前所处的位置信息,还要依靠下列的3种定位工具. (1)定位条件器Crite ...
- android gps 获取方位_Android GPS获取当前经纬度坐标
APP中可能会遇到一种需求,就是将当前所在位置的坐标传到服务器上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,第二种通过百度地图API来实现,第三种通过天地图AP ...
- 安卓开发入门gps获取定位经纬度海拔速度
layout xml 文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...
- 【Unity】中如何通过GPS获取设备经纬度(测试脚本)
在游戏开发中需要使用gps获取经纬度坐标定位玩家的当前位置,那么在开发过程中这个功能容不容易实现呢?下面就给大家介绍下Unity中获取设备经纬度的方法,一起来看看吧. Unity使用GPS 的API ...
- Android中如何使用GPS
Android中如何使用GPS获取位置信息?一个小Demo如下 GPS简介 Gobal Positioning System,全球定位系统,是美国在20世纪70年代研制的一种以人造地球卫星为基础的高精 ...
- 根据地址获取坐标经纬度
根据地址获取经纬度坐标 根据地址获取坐标经纬度 根据地址查经纬度坐标 目标:根据提供的一串地址的文字描述,查出此地址对应的经纬度. 例如你要查询『北京市西城区西长安街2号 』或『国家大剧院』的坐标. ...
- Android获取GPS网络定位经纬度信息
定位一般分为是:GPS定位,WIFI定位,基站定位 和 AGPS定位 GPS定位 GPS定位需要手机GPS模块硬件支持.GPS走的是卫星通信的通道,在没有网络连接的情况下也能使用,并且通过GPS方式准 ...
- 可运行的GPS获取经纬度和获取基站例子(环境Android Studio 3.5.2扒拉能运行的例子找到太辛苦了要么版本太老。)
可运行的GPS获取经纬度和获取基站例子(环境Android Studio 3.5.2扒拉能运行的例子找到太辛苦了要么版本太老.) 为了检测GPS和基站修改结果,结合网络例子.单独抠出来可运行实例,GP ...
- Android通过手机GPS获取经纬度方法
android 调用gps获取经纬度的方法大同小异,实则差不了多少.但是使用起来,有的方法看起来很冗杂,而且很不好用.下面为大家介绍中很简单的方法,而且是实时监听位置的变化. 首先定义: privat ...
- Android中获取定位经纬度信息
场景 根据GPS获取经纬度效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实 ...
最新文章
- Git 高级用法小抄
- MySQL: Root element is missing
- What's New In C# 6.0
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- Qt5模型/视图结构-视图(View)
- 【渝粤题库】国家开放大学2021春2143西方经济学题目
- 分解质因数-洛谷P3200 [HNOI2009]有趣的数列
- android开发常用技术,[转载]Android开发常用调试技术记录
- PTA: 7-2 银行业务队列简单模拟 (25 分)
- 【转载】Sqlserver使用Convert函数进行数据类型转换
- hbase put 写入数据慢_HBase运维 | HBase 疑难杂症诊治
- 【图像处理】H.264开源解码器评测
- 【预测模型】基于粒子群算法优化最小二乘支持向量机lssvm实现预测matlab源码
- 2节串联锂电池充电管理芯片IC,5V,12V升降压解决方案
- python类库包括_python类库大全
- Word文档没保存电脑死机了,重启打开文档一片空白怎么办?
- IDC:中国云计算市场超10亿 企业云火热
- 语雀批量导出与图片下载
- 编写各种outofmemory/stackoverflow程序
- 【DXP】更换原理图模板的方法
热门文章
- 轻松实现页面提交时,显示“提交中..”
- 【基础编程】最大子列和问题
- 网络子系统33_网桥设备的配置更新
- (dfs)[USACO3.4]“破锣摇滚”乐队 Raucous Rockers
- 胧月初音未来计算机,胧月(流星P所作歌曲《胧月》)_百度百科
- wps的ppt放映时不能完全全屏的解决方法
- linux中MIB与MB单位的区别
- 三步解决error: Microsoft Visual C++ 14.0 or greater is required. Get it with “Microsoft C++ Build Tools“
- C++ stander library--type traits and type utility
- [lua]紫猫lua教程-命令宝典-L1-03-01. 闭包