Seraph_浮生

导航

 

1.通过网页查询的网址(sensor=false参数代表,是否通过其他设备得到这个数据)

  a.通过地址查询经纬度  http://maps.googleapis.com/maps/api/geocode/json?address=SFO&sensor=false

  b.根据经纬度查询地址
  http://maps.googleapis.com/maps/api/geocode/json?latlng=30.523213,111.990881&sensor=false

  //bounds的作用(指定查询的经纬度区间)
  http://maps.googleapis.com/maps/api/geocode/json?Winnet&bounds=34.172684,118.604794|34.236144,119.641258&sensor=false

  //region的作用(指定查询地址的所属的国家)
  http://maps.googleapis.com/maps/api/geocode/json?address=Toledo&sensor=false
  http://maps.googleapis.com/maps/api/geocode/json?address=Toledo&sensor=false&region=es2.需要的权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

3.主要代码:

package seraph.location04;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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 com.google.gson.Gson;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
 * 用户定位(三)
 *  使用网页查询的方法代替GoogleAPI
 * @author Seraph
 *
 */
public class MainActivity extends Activity {
    private TextView textView = null;
    private Button addressbutton = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView = (TextView) findViewById(R.id.textViewId);
        addressbutton = (Button) findViewById(R.id.addressbuttonId);
        
        addressbutton.setOnClickListener(new OnClickListener() {
            /**
             * 根据经纬度得到地址
             *     使用网页查询的方式
             */
            @Override
            public void onClick(View v) {
                new addressTask().execute();
            }
        });
    }
    /**
     * 使用异步的方法访问网络
     *     通过经纬度得到地址
     */
    class addressTask extends AsyncTask<Void, Void, Void>{
        TempResult tempResult = null;
        @Override
        protected Void doInBackground(Void... params) {
            //通过地址查询经纬度,网页查询的地址 sensor参数,是否是手机得到的参数
            String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=30.523213,111.990881&sensor=false";
            //创建一个HttpClient对象
            HttpClient httpClient = new DefaultHttpClient();
            String responseData = "";
            try {
                //向指定的Url发送Http请求
                HttpResponse response = httpClient.execute(new HttpGet(url));
                //取得服务器返回的响应
                HttpEntity entity = response.getEntity();
                BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
                String line = "";
                while((line = br.readLine()) != null){
                    responseData += line;
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //使用Goolge解析
            Gson gson = new Gson();
            //解析的数据,和使用的类
            tempResult= gson.fromJson(responseData, TempResult.class);
            System.out.println("通过经纬度解析的地址为:\n"+tempResult);
            
            return null;
        }
        /**
         * 把结果输出到TextView
         */
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            textView.setText("通过经纬度解析的地址为:\n"+tempResult);
        }
        
    }
}

4.使用Google-Json解析查询到的结果。解析的类按照需要的数据进行定义。

posted on 2013-04-06 14:38  Seraph_浮生  阅读(611)  评论(0编辑  收藏  举报