您的位置:首页 > 其它

根据坐标查找

2013-09-14 21:18 218 查看

Geocode

  取得具体位置的坐标(经度和纬度)或者是取得位置的名称,
 
使用JSON会更好一些,因为传输的数据量更少。
 
在Google搜索上选择“更多”,然后选择“Code”,再选择“Google Maps API”,
接着选择“网络服务”,最后选择“Google Geocoding API”。
 

 




 

在main.xml中:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#3399ff">
  <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_margin="8dp">
     <TableRow>
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="输入坐标纬度:" />
       <EditText
         android:id="@+id/lat"
         android:layout_width="200px"
         android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow>
       <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="输入坐标经度:" />
       <EditText
         android:id="@+id/lng"
         android:layout_width="200px"
         android:layout_height="wrap_content"/>
     </TableRow>
     <TableRow>
       <Button
         android:id="@+id/search"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="检索周边信息" />
     </TableRow>
  </TableLayout>
  <TextView
     android:id="@+id/result"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" />
</LinearLayout>
 
 
 
 
在MyGeocodeDemo.java中:
 
package com.li.geocode;
 
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 
import org.json.JSONArray;
import org.json.JSONObject;
 
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class MyGeocodeDemo extends Activity {
  private EditText lng = null;
  private EditText lat = null;
  private Button search = null;
  private TextView result = null;
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     super.setContentView(R.layout.main);
     this.lng = (EditText) super.findViewById(R.id.lng);
     this.lat = (EditText) super.findViewById(R.id.lat);
     this.search = (Button) super.findViewById(R.id.search);
     this.result = (TextView) super.findViewById(R.id.result);
     this.search.setOnClickListener(new SearchOnClickListenerImpl());
  }
 
  private class SearchOnClickListenerImpl implements OnClickListener {
 
     public void onClick(View v) {
       double latitude = Double.parseDouble(MyGeocodeDemo.this.lat
            .getText().toString());
       double longitude = Double.parseDouble(MyGeocodeDemo.this.lng
            .getText().toString());
       MyGeocodeDemo.this.result.setText("请稍等,信息正在检索中...") ;
       new SearchAsyncTask(latitude,longitude).execute(0) ;
     }
 
  }
 
  // 此时要根据异步处理的工具类来向Google上进行数据的提交操作,而后通过返回的JSON数据解析出具体的数据内容
  private class SearchAsyncTask extends AsyncTask<Integer, String, Integer> {
     private double latitude;
    private double longitude;
 
     public SearchAsyncTask(double latitude, double longitude) {
       this.latitude = latitude;
       this.longitude = longitude;
     }
    
     @Override
     protected void onProgressUpdate(String... values) {
       MyGeocodeDemo.this.result.setText(values[0]) ;
     }
 
     @Override
     protected Integer doInBackground(Integer... arg0) {
       Map<String, String> map = null;
       try {
         map = this.parseJson(this.latitude, this.longitude);
       } catch (Exception e) {
         e.printStackTrace();
       }
       if("OK".equals(map.get("status"))) { // 操作正常
         this.publishProgress(map.get("address")) ;  // 将address数据返回
       } else {
         this.publishProgress("没有查询结果!") ;
       }
       return null;
     }
 
     private Map<String, String> parseJson(double latitude, double longitude)
         throws Exception {
       Map<String, String> allMap = new HashMap<String, String>();
       StringBuffer buf = new StringBuffer();
       InputStream input = null;
       try {
         URL url = new URL(
              "http://maps.google.com/maps/api/geocode/json?latlng="
                   + this.latitude + "," + this.longitude
                   + "&sensor=false");
         input = url.openStream();
         Scanner scan = new Scanner(input);
         while (scan.hasNext()) {
            buf.append(scan.next()); // 所有的数据都保存在字符串里面
         }
       } catch (Exception e) {
         e.printStackTrace();
         throw e;
       } finally {
         input.close() ;
       }
       JSONObject allData = new JSONObject(buf.toString());
       allMap.put("status", allData.getString("status")) ;
       JSONArray jsonArr = allData.getJSONArray("results") ;
       JSONObject jsonObj = jsonArr.getJSONObject(0) ;
       allMap.put("address", jsonObj.getString("formatted_address")) ;
       return allMap;
     }
  }
}
 
 
 
 
 
在AndroidManifest.xml中修改权限:
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.li.geocode"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
  <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyGeocodeDemo"
            android:label="@string/title_activity_my_geocode_demo" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  根据坐标查找