实现的功能:先获取本地的经纬度,再根据经纬度,请求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经纬度并用谷歌解析为地理位置名称相关推荐

  1. 【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)

    需要全部代码请点赞关注收藏后评论区留言私信~~~ 一.获取定位信息 开启定位相关功能只是将定位的前提条件准备好,若想获得手机当前所处的位置信息,还要依靠下列的3种定位工具. (1)定位条件器Crite ...

  2. android gps 获取方位_Android GPS获取当前经纬度坐标

    APP中可能会遇到一种需求,就是将当前所在位置的坐标传到服务器上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,第二种通过百度地图API来实现,第三种通过天地图AP ...

  3. 安卓开发入门gps获取定位经纬度海拔速度

    layout xml 文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...

  4. 【Unity】中如何通过GPS获取设备经纬度(测试脚本)

    在游戏开发中需要使用gps获取经纬度坐标定位玩家的当前位置,那么在开发过程中这个功能容不容易实现呢?下面就给大家介绍下Unity中获取设备经纬度的方法,一起来看看吧. Unity使用GPS 的API ...

  5. Android中如何使用GPS

    Android中如何使用GPS获取位置信息?一个小Demo如下 GPS简介 Gobal Positioning System,全球定位系统,是美国在20世纪70年代研制的一种以人造地球卫星为基础的高精 ...

  6. 根据地址获取坐标经纬度

    根据地址获取经纬度坐标 根据地址获取坐标经纬度 根据地址查经纬度坐标 目标:根据提供的一串地址的文字描述,查出此地址对应的经纬度. 例如你要查询『北京市西城区西长安街2号 』或『国家大剧院』的坐标. ...

  7. Android获取GPS网络定位经纬度信息

    定位一般分为是:GPS定位,WIFI定位,基站定位 和 AGPS定位 GPS定位 GPS定位需要手机GPS模块硬件支持.GPS走的是卫星通信的通道,在没有网络连接的情况下也能使用,并且通过GPS方式准 ...

  8. 可运行的GPS获取经纬度和获取基站例子(环境Android Studio 3.5.2扒拉能运行的例子找到太辛苦了要么版本太老。)

    可运行的GPS获取经纬度和获取基站例子(环境Android Studio 3.5.2扒拉能运行的例子找到太辛苦了要么版本太老.) 为了检测GPS和基站修改结果,结合网络例子.单独抠出来可运行实例,GP ...

  9. Android通过手机GPS获取经纬度方法

    android 调用gps获取经纬度的方法大同小异,实则差不了多少.但是使用起来,有的方法看起来很冗杂,而且很不好用.下面为大家介绍中很简单的方法,而且是实时监听位置的变化. 首先定义: privat ...

  10. Android中获取定位经纬度信息

    场景 根据GPS获取经纬度效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实 ...

最新文章

  1. webView loadData 中文乱码问题
  2. LLVM3.8停止了旧Windows版本,取消Autoconf,改进Clang
  3. 详解void 0与undefined区别
  4. 单细胞数据整合方法 | Comprehensive Integration of Single-Cell Data
  5. html表单的首要标记是form,关于html中表单form标记的介绍
  6. linux firefox 检查组件是否加载,利用火狐浏览器查看网站加载速度
  7. DevExpress统计图TextPattern说明
  8. 第一次个人项目【词频统计】——PSP表格
  9. 目录中带.造成文件上传验证问题
  10. Jbpm工作流表补数记录
  11. vb.net 正則表達式 取 固定格式的字符
  12. 目前软件分析设计过程中的主要问题
  13. 视频怎么转化成动态图?巧用视频转gif生成器
  14. android 定时关机 app,定时关机X下载-定时关机X(强制关机)下载v1.1 安卓版-西西软件下载...
  15. 乐橙tp1 html调用,乐橙TP1的妙用
  16. 自定义view画钟表
  17. 【SpringBoot学习】35、SpringBoot 简易文件服务器
  18. 电子计算机的发展世代
  19. Python基础之函数,面向对象
  20. OpenGL之glut、glfw、glew、glad等库之间的关系

热门文章

  1. 微信小程序之人脸识别
  2. 专访陈星汉:“游戏禅师” 的自我苛刻与孤独
  3. Gitee更新代码提示:master has no tracked branch
  4. MyBatis Plus 联合查询
  5. for循环中控制事务单个提交问题
  6. 印度公司注册数据[1857-2020]
  7. java会导致蓝屏么_电脑经常会蓝屏?可能是这些原因导致的
  8. OA系统-部门和员工管理模块
  9. 光伏行业MES管理系统解决方案
  10. 泛型:泛型类与泛型方法