您的位置:首页 > 移动开发 > Android开发

Android GSP定位获取经纬度并显示所在城市的名字(不用引入各种SDK)

2016-11-15 13:27 721 查看
Android GSP定位获取经纬度并显示所在城市的名字(不用引入各种sdk)
 主要是通过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" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐