首先 得到當前經緯度座標 傳到後臺

 

然後 後臺根據當前位置 和 資料庫中工程的經緯度比較 如果兩點之間的距離 <= 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端完整代碼如下:

 

layout檔:

 

<?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>

 

Activity檔:

 

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);

 

}

 

}

 

}
 
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()