(转)Android Location的使用!!
2013-04-08 16:42
369 查看
大家好,今天说说Location , Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发).等。而在Android 中通过LocationManager 来获取Location .通常获取Location 有GPS 获取,WIFI 获取。
我今天做一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度。下一节将教大家通过Location 来获取Address .
首先第一步:
创建一个Android 工程命名为LocationDemo .
第二步:修改main.xml 代码如下:
[java] view
plaincopy
<?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 ,代码如下:
[java] view
plaincopy
package com.android.tutor;
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;
}
}
第四步:增加权限,修改AndroidManifest.xml 代码如下(第16行为所增行):
[java] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tutor"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
第五步:运行LocationDemo 工程,所得效果如下(真机深圳测试):
![](http://hi.csdn.net/attachment/201006/15/0_1276610372yWz9.gif)
转自:http://blog.csdn.net/android_tutor/article/details/5672911
我今天做一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度。下一节将教大家通过Location 来获取Address .
首先第一步:
创建一个Android 工程命名为LocationDemo .
第二步:修改main.xml 代码如下:
[java] view
plaincopy
<?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 ,代码如下:
[java] view
plaincopy
package com.android.tutor;
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;
}
}
第四步:增加权限,修改AndroidManifest.xml 代码如下(第16行为所增行):
[java] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tutor"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
第五步:运行LocationDemo 工程,所得效果如下(真机深圳测试):
![](http://hi.csdn.net/attachment/201006/15/0_1276610372yWz9.gif)
转自:http://blog.csdn.net/android_tutor/article/details/5672911
相关文章推荐
- Android Location的使用
- Android 位置服务——BaiduLocation的使用
- Android高手进阶教程(十五)之---通过Location获取Address的使用!
- Android 6.0 使用蓝牙BLE 时 报错:java.lang.SecurityException:Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOC
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 6.0 使用蓝牙BLE 时 报错:java.lang.SecurityException:Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOC
- [009] 百度地图API之MyLocationOverlay的使用(Android)
- Android中的ACCESS_MOCK_LOCATION权限使用Demo
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 高手进阶教程(十三)----Android Location的使用!!
- Android使用Google提供的地图实现定位时LocationListener的各个方法
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- 百度地图API之MyLocationOverlay的使用(Android)
- Android LocationManager 使用
- 百度地图4之MyLocationOverlay的使用(Android)
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用