前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >android开发_Location位置定位

android开发_Location位置定位

作者头像
Hongten
发布2018-09-13 15:58:40
1K0
发布2018-09-13 15:58:40
举报
文章被收录于专栏:Hongten

新建项目:

代码语言:javascript
复制
1 New Android Project->
2 Project name:Location
3 Build Target:Android 2.2
4 Application name: AppWidget
5 Package name:com.b510
6 Create Activity:MainActivity
7 Min SDK Version:9
8 Finish

项目结构:

运行效果:

代码部分:

AndroidManifest.xml

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3       package="com.b510"
 4       android:versionCode="1"
 5       android:versionName="1.0">
 6     <uses-sdk android:minSdkVersion="9" />
 7 
 8     <application android:icon="@drawable/icon" android:label="@string/app_name">
 9         <activity android:name=".MainActivity"
10                   android:label="@string/app_name">
11             <intent-filter>
12                 <action android:name="android.intent.action.MAIN" />
13                 <category android:name="android.intent.category.LAUNCHER" />
14             </intent-filter>
15         </activity>
16 
17     </application>
18     <!-- from android api:Allows an application to access fine (e.g., GPS) location  -->
19     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
20 </manifest>

main.xml

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7         <!-- 表示经度 -->
 8     <TextView  
 9         android:id="@+id/longitutde"
10         android:layout_width="fill_parent" 
11         android:layout_height="wrap_content" 
12         />
13           <!-- 表示纬度 -->
14     <TextView  
15         android:id="@+id/latitude"
16         android:layout_width="fill_parent" 
17         android:layout_height="wrap_content" 
18         />
19 </LinearLayout>

MainActivity.java

代码语言:javascript
复制
 1 package com.b510;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.location.Location;
 6 import android.location.LocationManager;
 7 import android.os.Bundle;
 8 import android.widget.TextView;
 9 
10 public class MainActivity extends Activity {
11     /** 显示经度 */
12     private TextView longitude;
13     /** 显示维度 */
14     private TextView latitude;
15 
16     /** Called when the activity is first created. */
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21 
22         //从xml中找到已经定义好的TextView:longitude,latitude
23         longitude = (TextView) findViewById(R.id.longitutde);
24         latitude = (TextView) findViewById(R.id.latitude);
25         //初始化一个location
26         Location location = getLocation(this);
27         //设置TextView控件的显示信息:经度和维度
28         longitude.setText("经度:" + location.getLongitude());
29         latitude.setText("维度:" + location.getLatitude());
30     }
31     @SuppressWarnings("static-access")
32     private Location getLocation(Context context) {
33         //You do not instantiate this class directly;
34         //instead, retrieve it through: 
35         //Context.getSystemService(Context.LOCATION_SERVICE).
36         LocationManager locationManager = (LocationManager) context
37                 .getSystemService(context.LOCATION_SERVICE);
38         //获取GPS支持
39         Location location = locationManager
40                 .getLastKnownLocation(locationManager.GPS_PROVIDER);
41         if (location == null) {
42             //获取NETWORK支持
43             location = locationManager
44                     .getLastKnownLocation(locationManager.NETWORK_PROVIDER);
45         }
46         return location;
47     }
48 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2012-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档