天天看點

Android GSP定位擷取經緯度并顯示所在城市的名字(不用引入各種SDK)

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" />