public
class
MainActivity
extends
Activity{
ArrayList<HashMap<String, Object>> searchResults;
ArrayList<HashMap<String, Object>> originalValues;
private
CustomAdapter adapter;
LayoutInflater inflater;
double
x,y;
Timer timer;
LocationManager lm;
boolean
gps_enabled =
false
;
boolean
network_enabled =
false
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
final
EditText searchBox = (EditText) findViewById(R.id.et_search_name);
final
ListView insurListView = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
String names[] = {
"K"
,
"J"
,
"L"
,
"O"
,
"S"
,
};
String calls[] = {
"11111111"
,
"22222222"
,
"33333333"
,
"44444444"
,
"55555555"
,
};
Double lati[] = {
35.000001
,
26.000001
,
67.000001
,
15.000001
,
79.000001
,
};
Double longi[] = {
129.000001
,
159.000001
,
100.000001
,
131.000001
,
162.000001
,
};
originalValues =
new
ArrayList<HashMap<String, Object>>();
HashMap<String, Object> temp;
int
oriname = names.length;
for
(
int
i =
0
; i < oriname; i++) {
temp =
new
HashMap<String, Object>();
temp.put(
"name"
, names[i]);
temp.put(
"call"
, calls[i]);
temp.put(
"lati"
, lati[i]);
temp.put(
"longi"
, longi[i]);
originalValues.add(temp);
}
searchResults =
new
ArrayList<HashMap<String, Object>>(originalValues);
adapter =
new
CustomAdapter(
this
,R.layout.list_row, searchResults);
insurListView.setAdapter(adapter);
searchBox.addTextChangedListener(
new
TextWatcher() {
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {
String searchString = searchBox.getText().toString();
int
textLength = searchString.length();
searchResults.clear();
for
(
int
i =
0
; i < originalValues.size(); i++) {
String insurName = originalValues.get(i).get(
"name"
).toString();
if
(textLength <= insurName.length()) {
if
(searchString.equalsIgnoreCase(insurName.substring(
0
, textLength)))
searchResults.add(originalValues.get(i));
}
}
adapter.notifyDataSetChanged();
}
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after) {
}
public
void
afterTextChanged(Editable s) {
}
});
lm = (LocationManager)
this
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if
(!gps_enabled && !network_enabled) {
Context context = getApplicationContext();
int
duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,
"nothing is enabled"
, duration);
toast.show();
}
if
(gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0
,
0
,
locationListenerGps);
if
(network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0
,
0
,
locationListenerNetwork);
}
private
class
CustomAdapter
extends
ArrayAdapter<HashMap<String, Object>> {
public
CustomAdapter(Context context,
int
textViewResourceId,
ArrayList<HashMap<String, Object>> Strings) {
super
(context, textViewResourceId, Strings);
}
private
class
ViewHolder {
TextView name;
TextView call;
TextView distance;
}
ViewHolder viewHolder;
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
double
sLat = Double.valueOf(searchResults.get(position).get(
"lati"
).toString());
double
sLng = Double.valueOf(searchResults.get(position).get(
"longi"
).toString());
double
eLat = Double.valueOf(x);
double
eLng = Double.valueOf(y);
if
(x==
0
){
sLat =
0
;
sLng =
0
;
}
String distance = SocialUtil.calcDistance(sLat, sLng, eLat, eLng);
if
(convertView ==
null
) {
convertView = inflater.inflate(R.layout.list_row,
null
);
viewHolder =
new
ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.name);
viewHolder.call = (TextView) convertView.findViewById(R.id.call);
viewHolder.distance = (TextView) convertView.findViewById(R.id.distance);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) convertView.getTag();
final
int
pos = position;
viewHolder.name.setText(searchResults.get(position).get(
"name"
).toString());
viewHolder.call.setText(searchResults.get(position).get(
"call"
).toString());
viewHolder.distance.setText(distance);
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);
adapter.notifyDataSetChanged();
Context context = getApplicationContext();
int
duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,
"gps enabled "
+x +
"\n"
+ y, duration);
toast.show();
}
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);
adapter.notifyDataSetChanged();
Context context = getApplicationContext();
int
duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context,
"network enabled"
+x +
"\n"
+ y, duration);
toast.show();
}
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;
}
}
}