android GPS获取地理信息(弥补Geocoder不足之处)


android GPS获取地理信息(弥补Geocoder不足之处)
 最近做一个社交软件、需要用到GPS、但是过程中有反向编码、也有正向编码、

但是很多时候Geocoder根本获取不到、始终是为空、经过本人二十四小时的不断baidu+google终于查看到其实这个Geocoder类并不是拿不到数据、只是访问能拿到数据的次数非常的小、

我放弃了该种获取经纬度取地址、也放弃了地址取经纬度

于是在一篇博客中偶然看到一个关于google的api的说法

关于用网络来拿到数据(网子如下)

https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN#GeocodingRequests获得google的GPS服务的api文档

后来

次过程中将用到json解析数据、其中的非常简单、相信各位看本篇博客的人已经能找到json解析了

废话少说了准备上代码、如果有什么不懂本人候教

 

 

以下是本人做出来的一个关键类、

package com.hhj.gps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

/*import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;*/

import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GoogleGeographyQuery{
	static double lng;
	static double lat;
	/*查询经纬度*/
	public static double[] jingweidu(String AddressName){
		//List<Map<String, Object>> mData=new ArrayList<Map<String, Object>>();
		String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + AddressName + "&sensor=false";
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);
		try {
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			InputStream input = entity.getContent();
			int t;
			StringBuffer buffer = new StringBuffer();
			while ((t = input.read()) != -1) {
				buffer.append((char) t);
				
			}
			//tv.setText(buffer);

			// json解析
			JSONObject object = new JSONObject(buffer.toString());
			JSONObject location = object.getJSONArray("results")
					.getJSONObject(0)// 获得中括号的内容
					.getJSONObject("geometry")// 获得大括号中的内容
					.getJSONObject("location");
			lng = location.getDouble("lng");
			lat = location.getDouble("lat");
			Log.i("HHJ", "经纬度是 : "+lng+"   "+lat);
			double[] data ={lng,lat};
			return data;

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	
	}
	
	public static String geocodeAddr(String latitude, String longitude) {
		String addr = "";
		// 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址
		// 密钥可以随便写一个key=abc
		// output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析
		String url = String.format("http://ditu.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",latitude, longitude);
		URL myURL = null;
		URLConnection httpsConn = null;
		try {
			myURL = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}

		try {
			httpsConn = (URLConnection) myURL.openConnection();
			if (httpsConn != null) {
				InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
				BufferedReader br = new BufferedReader(insr);
				String data = null;
				if ((data = br.readLine()) != null) {
					System.out.println(data);
					String[] retList = data.split(",");
					if (retList.length > 2 && ("200".equals(retList[0]))) {
						addr = retList[2];
						addr = addr.replace("\"", "");
					} else {
						addr = "";
					}
					Log.i("HHJ", "123  : "+addr);
				}
				insr.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return addr;
	}

}

 

如果需要demo源码请致电[email protected](附带本博客名字)

你可能感兴趣的:(android,gps)