如何自动找到当前的经纬度值?
Posted
技术标签:
【中文标题】如何自动找到当前的经纬度值?【英文标题】:How to find the current longitude and latitude value automatically? 【发布时间】:2014-03-03 08:43:29 【问题描述】:我正在尝试制作一款可以提供最近的医院名称和地址的应用程序。我正在使用 Json 解析。这是我的代码..
我正在使用这个 url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=17.4367624,78.439968799&radius=5000&types=hospital&sensor=true&key=AIzaSyAwBKDflSlg9d38GX1xrwpnwcbWCzdVj-A"
我这个 longi 和 lati 值是手动给出的,但我希望无论用户去哪里,它都应该根据当前位置采用相应的 lat & long 值。请帮助我如何解决这个问题......任何帮助都会很明显......谢谢......
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;
【问题讨论】:
试试下面的链接:***.com/questions/2227292/… 尽管它的目的不同,你只是想得到电话的经度和纬度。该链接是第一个谷歌点击,看起来很可靠。 【参考方案1】:看看这个指南:
https://developer.android.com/training/location/index.html
如果您关注它,您将收到位置更新并可以将其放入请求字符串中。
【讨论】:
【参考方案2】:试试这个以获得当前位置的详细信息以获取更多详细信息关注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;
【讨论】:
以上是关于如何自动找到当前的经纬度值?的主要内容,如果未能解决你的问题,请参考以下文章