[Android实例] Google地图经纬度转换成具体位置

package com.wht.location;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class LocationDemoActivity extends Activity implements OnClickListener
{
        private final double lat = 31.9777;
        private final double lng = 118.767481;
        private TextView addressTView;
        private Button parseBtn;


        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                parseBtn = (Button) findViewById(R.id.address_parse);
                addressTView = (TextView) findViewById(R.id.address);


                parseBtn.setOnClickListener(this);


        }


        /**
         * 由经纬度获得地址
         *
         * @param latitude
         * 纬度
         * @param longitude
         * 经度
         * @return
         */


        private static JSONObject geocodeAddr(double lat, double lng)
        {
                String urlString = "http://ditu.google.com/maps/geo?q=+" + lat + ","
                                + lng + "&output=json&oe=utf8&hl=zh-CN&sensor=false";
                //String urlString = "http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&language=zh_CN&sensor=false";
                StringBuilder sTotalString = new StringBuilder();
                try
                {


                        URL url = new URL(urlString);
                        URLConnection connection = url.openConnection();
                        HttpURLConnection httpConnection = (HttpURLConnection) connection;


                        InputStream urlStream = httpConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(
                                        new InputStreamReader(urlStream));


                        String sCurrentLine = "";
                        while ((sCurrentLine = bufferedReader.readLine()) != null)
                        {
                                sTotalString.append(sCurrentLine);
                        }
                        bufferedReader.close();
                        httpConnection.disconnect(); // 关闭http连接


                }
                catch (Exception e1)
                {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }


                JSONObject jsonObject = new JSONObject();
                try
                {
                        jsonObject = new JSONObject(sTotalString.toString());
                }
                catch (JSONException e)
                {
                        e.printStackTrace();
                }


                return jsonObject;
        }


        private static String getAddressByLatLng(double lat, double lng)
        {
                String address = null;
                JSONObject jsonObject = geocodeAddr(lat, lng);
                try
                {
                        JSONArray placemarks = jsonObject.getJSONArray("Placemark");
                        JSONObject place = placemarks.getJSONObject(0);
                        address = place.getString("address");
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
                return address;
        }


        @Override
        public void onClick(View v)
        {
                switch (v.getId())
                {
                case R.id.address_parse:
                        String address = getAddressByLatLng(lat, lng);
                        addressTView.setText(address);
                        break;


                default:
                        break;
                }
        }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值