google经纬度互转

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

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;
    }

}

猜你喜欢

转载自f059074251.iteye.com/blog/1903899