Android GSP定位获取经纬度并显示所在城市的名字(不用引入各种SDK)
2016-11-15 13:27
721 查看
Android GSP定位获取经纬度并显示所在城市的名字(不用引入各种sdk)
主要是通过GPS定位来获取经纬度,然后通过经纬度转换成所在的城市:
布局就是一个显示经纬度,一个显示城市:
权限:
主要是通过GPS定位来获取经纬度,然后通过经纬度转换成所在的城市:
package com.uucwerewolf.map; import java.io.IOException; import java.util.List; import android.app.Activity; import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { String latLongString; Double a, b; private TextView city; private TextView showJW = null; private LocationManager locationManager; private double latitude = 0; private double longitude = 0; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { double[] data = (double[]) msg.obj; showJW.setText("经度:" + data[0] + "\t纬度:" + data[1]); List<Address> addList = null; Geocoder ge = new Geocoder(getApplicationContext()); try { addList = ge.getFromLocation(data[0], data[1], 1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (addList != null && addList.size() > 0) { for (int i = 0; i < addList.size(); i++) { Address ad = addList.get(i); latLongString = ad.getLocality(); } } city.setText(latLongString); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showJW = (TextView) findViewById(R.id.showJW); city = (TextView) findViewById(R.id.city); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); new Thread() { @Override public void run() { Location location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); // 经度 longitude = location.getLongitude(); // 纬度 double[] data = { latitude, longitude }; Message msg = handler.obtainMessage(); msg.obj = data; handler.sendMessage(msg); } } }.start(); } /** * 确定按钮监听 * * @param view */ public void getJW(View view) { new Thread() { @Override public void run() { Location location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); // 经度 longitude = location.getLongitude(); // 纬度 double[] data = { latitude, longitude }; Message msg = handler.obtainMessage(); msg.obj = data; handler.sendMessage(msg); } } }.start(); } }
布局就是一个显示经纬度,一个显示城市:
<RelativeLayout 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" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:id="@+id/showJW" android:text="@string/hello_world" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="获得经纬度" android:onClick="getJW" /> <TextView android:id="@+id/city" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginBottom="69dp" android:text="aaaaaa" android:textColor="#000000" /> </RelativeLayout>
权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
相关文章推荐
- android 根据经纬度定位所在城市
- android定位已获取经纬度,上传google解析后textview不显示,基础问题,求大神解答~~>_<`
- 百度Android定位SDK实现获取当前经纬度及位置
- (转载)百度Android定位SDK实现获取当前经纬度及位置
- windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息)
- Android LocationManager定位之获取经纬度以及城市信息
- android 根据经纬度定位所在城市
- android开发游记:集成百度定位SDK进行实时定位 获取当前城市地理信息
- iOS 自带定位最新获取 街区名字 、街道名字、城市名字、省份等,并获取当经纬度。
- windows phone 7 定位(获取经纬度),然后找到经纬度所在的位置(城市信息)
- 百度Android定位SDK实现获取当前经纬度及位置
- Android定位获取经纬度并显示位置
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- Android利用百度定位SDK获取当前城市位置
- android通过高德地图定位sdk获取当前位置的经纬度(只是经纬度哦)
- Android获取手机内置外置存储文件列表,完美解决各种rom各种系统版本的获取方式不用的问题
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- 百度定位SDK实现获取当前经纬度及位置
- Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等