import
java.util.ArrayList;
public
class
MainActivity
extends
Activity {
double
x,y;
Timer timer;
LocationManager lm;
boolean
gps_enabled =
false
;
boolean
network_enabled =
false
;
private
CustomAdater adapter;
private
ArrayList<MyItem> arrayItems;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
double
eLat = Double.valueOf(x);
double
eLng = Double.valueOf(y);
arrayItems =
new
ArrayList<MyItem>();
arrayItems.add(
new
MyItem(
"Tom01"
, SocialUtil.calcDistance(
35.000001
,
129.000001
, eLat, eLng)));
arrayItems.add(
new
MyItem(
"Tom02"
, SocialUtil.calcDistance(
26.000001
,
159.000001
, eLat, eLng)));
arrayItems.add(
new
MyItem(
"Tom03"
, SocialUtil.calcDistance(
67.000001
,
100.000001
, eLat, eLng)));
arrayItems.add(
new
MyItem(
"Tom04"
, SocialUtil.calcDistance(
15.000001
,
131.000001
, eLat, eLng)));
arrayItems.add(
new
MyItem(
"Tom05"
, SocialUtil.calcDistance(
79.000001
,
162.000001
, eLat, eLng)));
adapter =
new
CustomAdater(
this
, R.layout.list_row, arrayItems);
ListView list = (ListView)
this
.findViewById(android.R.id.list);
list.setAdapter(adapter);
lm = (LocationManager)
this
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if
(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0
,
0
,
locationListenerGps);
if
(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0
,
0
,
locationListenerNetwork);
timer=
new
Timer();
timer.schedule(
new
GetLastLocation(),
20000
);
}
public
class
CustomAdater
extends
BaseAdapter {
Context context;
int
itemlayout;
ArrayList<MyItem> arrayItems;
LayoutInflater inflater;
public
CustomAdater(Context c,
int
sublayout, ArrayList<MyItem> list) {
this
.context = c;
this
.itemlayout = sublayout;
this
.arrayItems = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public
int
getCount() {
return
arrayItems.size();
}
@Override
public
Object getItem(
int
position) {
return
arrayItems.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
final
int
pos = position;
if
(convertView ==
null
) {
convertView = inflater.inflate(itemlayout, parent,
false
);
}
TextView name = (TextView) convertView.findViewById(R.id.list_name);
TextView dis = (TextView) convertView.findViewById(R.id.list_distance);
name.setText(arrayItems.get(position).mname);
dis.setText(arrayItems.get(position).mdis);
return
convertView;
}
}
LocationListener locationListenerGps =
new
LocationListener() {
public
void
onLocationChanged(Location location) {
timer.cancel();
x =location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(
this
);
lm.removeUpdates(locationListenerNetwork);
arrayItems.clear();
arrayItems.addAll(arrayItems)
adapter.notifyDataSetChanged();
}
public
void
onProviderDisabled(String provider) {}
public
void
onProviderEnabled(String provider) {}
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {}
};
LocationListener locationListenerNetwork =
new
LocationListener() {
public
void
onLocationChanged(Location location) {
timer.cancel();
x = location.getLatitude();
y = location.getLongitude();
lm.removeUpdates(
this
);
lm.removeUpdates(locationListenerGps);
arrayItems.clear();
arrayItems.addAll(arrayItems)
adapter.notifyDataSetChanged();
}
public
void
onProviderDisabled(String provider) {}
public
void
onProviderEnabled(String provider) {}
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {}
};
public
static
class
SocialUtil {
public
static
String calcDistance(
double
lat1,
double
lon1,
double
lat2,
double
lon2){
double
EARTH_R, Rad, radLat1, radLat2, radDist;
double
distance, ret;
EARTH_R =
6371000.0
; Rad = Math.PI/
180
; radLat1 = Rad * lat1;
radLat2 = Rad * lat2; radDist = Rad * (lon1 - lon2);
distance = Math.sin(radLat1) * Math.sin(radLat2);
distance = distance + Math.cos(radLat1) * Math.cos(radLat2) * Math.cos(radDist);
ret = EARTH_R * Math.acos(distance);
double
rslt = Math.round(Math.round(ret) /
1000
);
String result = rslt +
" km"
;
if
(rslt ==
0
) result = Math.round(ret) +
" m"
;
return
result;
}
}
public
class
MyItem {
String mname, mdis;
public
MyItem(String name, String dis){
mname = name;
mdis = dis;
}
}
}