您的位置:首页 > 移动开发 > Android开发

android开发_Location位置定位

2012-11-19 22:14 357 查看
新建项目:

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐