根据坐标查找
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>
相关文章推荐
- tpshop商城根据用户坐标,向数据库查找附近的商家
- tpshop商城根据用户坐标,向数据库查找附近的商家
- 根据百度地图JSAPI查找某个地址的经纬度坐标
- 如何根据文件的inode号查找inode数据
- 通过区域坐标在mongodb中查找范围内的坐标点
- iOS开发- 根据表格点击tableviewCell获取在当前屏幕中的坐标值
- 根据闭合的多边形坐标求面积
- [SVG] 根据SVG椭圆弧路径参数计算中心点坐标、起始角度、结束角度的Javascript函数
- php 根据ip查找城市
- 根据数据库文件,查找对应的数据库
- MySQL根据某一个或者多个字段查找重复数据
- 根据表名查找所在的数据库名(用游标)
- Implementations of interface through Reflection 反射根据继承的信息查找指定的类
- 自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画
- 23. 根据公司查找底下的所有部门,根据部门找对应的公司
- wpf 根据当前控件,查找父窗口,调用父窗口的方法
- 如何根据事物代码查找相应BAPI
- Android 根据坐标获取控件方法
- 《设计模式之禅》中根据一个接口查找所有实现类
- EXCEL数据根据XY坐标导入ArcGIS