我正在尝试制作一个可以提供最近医院名称和地址的应用程序。我正在使用Json解析。这是我的代码..
我这个longi和lati值是手动给出的,但是我想要用户将去哪里,它应该采取相应的lat&根据当前位置的长值。请帮助我如何处理这个......任何帮助都会很明显..谢谢..
public class MainActivity extends Activity {
JSONArray jsonArray;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4367624,78.439968799&radius=5000&types=hospital&sensor=true&key=AIzaSyAwBKDflSlg9d38GX1xrwpnwcbWCzdVj-A");
north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo");
try {
HttpResponse response = client.execute(post);
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
jsonArray = new JSONArray(jsonResult);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView list = (ListView) findViewById(R.id.listView1);
List_Adapter la = new List_Adapter();
list.setAdapter(la);
}
class List_Adapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return jsonArray.length();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int index, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v = getLayoutInflater().inflate(R.layout.custom, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
try {
tv.append("id : "
+ jsonArray.getJSONObject(index).getString("id")
.toString() + "\n");
tv.append("level: "
+ jsonArray.getJSONObject(index).getString("level")
.toString() + "\n");
tv.append("time_in_secs: "
+ jsonArray.getJSONObject(index)
.getString("time_in_secs").toString() + "\n");
tv.append("par: "
+ jsonArray.getJSONObject(index).getString("par")
.toString() + "\n");
tv.append("initials: "
+ jsonArray.getJSONObject(index).getString("initials")
.toString() + "\n");
tv.append("quote: "
+ jsonArray.getJSONObject(index).getString("quote")
.toString() + "\n");
tv.append("time_stamp: "
+ jsonArray.getJSONObject(index)
.getString("time_stamp").toString() + "\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return v;
}
}
private StringBuilder inputStreamToString(InputStream is) {
// TODO Auto-generated method stub
String rLine = "";
StringBuilder sb = new StringBuilder();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
try {
while ((rLine = br.readLine()) != null) {
sb.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb;
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个获取当前位置详细信息以获取更多详细信息请点击android developer site。您可以获得有关位置感知的完整文档。
首先在清单中指定应用程序权限以访问当前的活动
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
检索当前位置的代码
LocationManager locationManager;
double current_latitude,current_longitude;
String location_context;
Location location;
double[] find_Location() {
location_context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)activity.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,
new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider) {
Toast.makeText(activity.getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(activity.getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
});
location = locationManager.getLastKnownLocation(provider);
if (location != null) {
current_latitude = location.getLatitude();
current_longitude = location.getLongitude();
}
}
return new double[]{current_latitude,current_longitude};
}