2010.12.10(4)——— android 1000范围内的数据以及两点之间的距离
项目中 需要在手机上显示一定范围内的 工程信息 在网上找了好久 没找到合适的 只好自己想了
思路:
首先 得到当前经纬度坐标 传到后台
然后 后台根据当前位置 和 数据库中工程的经纬度比较 如果两点之间的距离 <= 1000米 就显示在手机上
难点 :
主要 是计算两点之间的距离 这个 网上例子很多 如下:
这个就是服务器端的主要代码了
android端完整代码如下:
[b]layout文件:[/b]
[b]Activity文件:[/b]
项目中 需要在手机上显示一定范围内的 工程信息 在网上找了好久 没找到合适的 只好自己想了
思路:
首先 得到当前经纬度坐标 传到后台
然后 后台根据当前位置 和 数据库中工程的经纬度比较 如果两点之间的距离 <= 1000米 就显示在手机上
难点 :
主要 是计算两点之间的距离 这个 网上例子很多 如下:
public class MapDistance {
private static double EARTH_RADIUS = 6378.137;
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
}
这个就是服务器端的主要代码了
android端完整代码如下:
[b]layout文件:[/b]
<?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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="40dip"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/sp_jl"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="20dip"
android:layout_height="wrap_content"
android:text="序号"
android:textColor="#f0f4f9"
/>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="工程名称"
android:textColor="#ffffff"
/>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="建设单位"
android:textColor="#ffffff"
/>
<TextView
android:layout_width="50dip"
android:layout_height="wrap_content"
android:text="施工状态"
android:textColor="#ffffff"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="建设性质"
android:textColor="#ffffff"
/>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dis_listview"
/>
</LinearLayout>
[b]Activity文件:[/b]
package com.huitu.project;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.huitu.pojo.GCXX2;
import com.huitu.service.GCXXService;
import com.huitu.util.JSONUtil;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
public class GCXXForLatLng extends Activity {
private Spinner sp_jl;
private ListView listView;
private String flat;
private ProgressDialog pd;
private Handler mProgressHandler;
private SimpleAdapter adapter;
private MyThread mt ;
private LocationManager locationManager;
private String lat;
private String lng;
private String jl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gcxxforlatlng);
pd = new ProgressDialog(this);
pd.setIndeterminate(true);
pd.setMessage("加载数据...");
pd.setCancelable(true);
pd.show();
mProgressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
int length = msg.getData().getInt("length");
switch (length){
case 1 :
pd.dismiss();
break;
case 2 :
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ItemClickListen());
break;
}
}
};
Intent intent = this.getIntent();
flat = intent.getStringExtra("flat");
sp_jl = (Spinner)findViewById(R.id.sp_jl);
listView = (ListView)findViewById(R.id.dis_listview);
String[] jls = {"500","1000","2000"};
ArrayAdapter arr_jl = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, jls);
arr_jl.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_jl.setAdapter(arr_jl);
sp_jl.setPrompt("请选择查询距离");
// 默认选择1000米
sp_jl.setSelection(1, true);
sp_jl.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pd.show();
jl = (String) sp_jl.getSelectedItem();
mt = new MyThread();
mt.start();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
pd.show();
lat = location.getLatitude()+"";
lng = location.getLongitude()+"";
jl = "1000";
mt = new MyThread();
mt.start();
LocationListener ll = new LocationListener(){
public void onLocationChanged(Location location) {
pd.show();
lat = location.getLatitude()+"";
lng = location.getLongitude()+"";
mt = new MyThread();
mt.start();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
locationManager.requestLocationUpdates(provider, 500, 5,
ll);
}
private final class ItemClickListen implements OnItemClickListener{
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ListView lv = (ListView)parent;
HashMap<String, Object> map = (HashMap<String, Object>)lv.getItemAtPosition(position);
String GCBM = map.get("GCBM").toString();
Intent intent = null;
if(flat.equals("0")){
intent = new Intent(GCXXForLatLng.this,XZJLListActivity.class);
}else{
intent = new Intent(GCXXForLatLng.this,XZJL_UploadActivity.class);
}
Bundle bundle = new Bundle();
bundle.putString("GCBM", GCBM);
intent.putExtras(bundle);
startActivity(intent);
}
}
private void updateView(String lat,String lng,String jl){
try {
String json = GCXXService.query(lat,lng,jl);
System.out.println(json);
List<GCXX2> list = JSONUtil.parseJSON_GCXX2_list(json);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
// String[]sta={"无证","有证"};
// String s;
for (GCXX2 bean : list) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("RN", bean.getRowId());
item.put("GCMC", bean.getXMMC());
item.put("JSDWMC", bean.getJSDWMC());
item.put("GCZT_NAME", bean.getGCZT_NAME());
item.put("STATUES", bean.getSTATUES());
item.put("GCXZ", bean.getGCXZNAME());
item.put("GCBM", bean.getGCBM());
data.add(item);
}
adapter = new SimpleAdapter(GCXXForLatLng.this, data,
R.layout.gcxxitem, new String[] { "RN", "GCMC", "JSDWMC",
"GCZT_NAME", "GCXZ" }, new int[] {
R.id.gcxx_rn, R.id.gcmc, R.id.jsdw, R.id.gczt,
R.id.jsxz });
// ll.setVisibility(View.VISIBLE);
Message msg = new Message();
msg.getData().putInt("length", 2);
mProgressHandler.sendMessage(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class MyThread extends Thread {
public void run(){
updateView(lat,lng,jl);
Message msg = new Message();
msg.getData().putInt("length", 1);
mProgressHandler.sendMessage(msg);
}
}
}