Android获取经纬度
Location在Android开发中还是经常用到的,比如通过经纬度获取天气,根据Location获取所在地区详细Address(比如GoogleMap开发).等。而在Android中通过LocationManager来获取Location.通常获取Location有GPS获取,WIFI获取。
第一步:
创建一个Android工程命名为LocationDemo.
第二步:修改main.xml代码如下:
<?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" > <TextView android:id="@+id/longitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="longitude:" /> <TextView android:id="@+id/latitude" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="latitude:" /> </LinearLayout>
第三步:修改LocationDemo.java,代码如下:
package cn.caiwb.location; import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; public class LocationDemo extends Activity { private TextView longitude; private TextView latitude; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); longitude = (TextView)findViewById(R.id.longitude); latitude = (TextView)findViewById(R.id.latitude); Location mLocation = getLocation(this); longitude.setText("Longitude: " + mLocation.getLongitude()); latitude.setText("Latitude: " + mLocation.getLatitude()); } //Get the Location by GPS or WIFI public Location getLocation(Context context) { LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null) { location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } return location; } }
第四步:增加权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
相关推荐
Nostalgiachild 2020-11-13
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30