android开发_Location位置定位
2012-11-19 22:14
357 查看
新建项目:
项目结构:
运行效果:
代码部分:
AndroidManifest.xml
main.xml
MainActivity.java
New Android Project-> Project name:Location Build Target:Android 2.2 Application name: AppWidget Package name:com.b510 Create Activity:MainActivity Min SDK Version:9 Finish
项目结构:
运行效果:
代码部分:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.b510" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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> <!-- from android api:Allows an application to access fine (e.g., GPS) location --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> </manifest>
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/longitutde" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 表示纬度 --> <TextView android:id="@+id/latitude" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.b510; 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 MainActivity extends Activity { /** 显示经度 */ private TextView longitude; /** 显示维度 */ private TextView latitude; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //从xml中找到已经定义好的TextView:longitude,latitude longitude = (TextView) findViewById(R.id.longitutde); latitude = (TextView) findViewById(R.id.latitude); //初始化一个location Location location = getLocation(this); //设置TextView控件的显示信息:经度和维度 longitude.setText("经度:" + location.getLongitude()); latitude.setText("维度:" + location.getLatitude()); } @SuppressWarnings("static-access") private Location getLocation(Context context) { //You do not instantiate this class directly; //instead, retrieve it through: //Context.getSystemService(Context.LOCATION_SERVICE). LocationManager locationManager = (LocationManager) context .getSystemService(context.LOCATION_SERVICE); //获取GPS支持 Location location = locationManager .getLastKnownLocation(locationManager.GPS_PROVIDER); if (location == null) { //获取NETWORK支持 location = locationManager .getLastKnownLocation(locationManager.NETWORK_PROVIDER); } return location; } }
相关文章推荐
- android开发_Location位置定位
- [android] 百度地图开发 (三).定位当前位置及getLastKnownLocation获取location总为空问题
- [android] 百度地图开发 (二).定位城市位置和城市POI搜索
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android之百度地图开发,包含定位,覆盖物,经纬度位置转换,长按设置中心位置
- Android GIS开发系列-- 入门季(9) 定位当前的位置
- Android 位置服务——用户定位User Location
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 利用高德地图API进行定位、开发电子围栏、天气预报、轨迹记录、搜索周边(位置)
- 基于即时通信和LBS技术的位置感知服务(四):Location-Aware-Instant:开发即时位置感知的Android客户端
- [IOS地图开发系类]1、CLLocationManager 位置定位
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
- Android"挂逼"修炼之行---微信中定位照片的位置信息插件开发详解