大家好,今天说说Location, Location在Android开发中还是经常用到的,比如 通过经纬度获取天气,根据Location获取所在地区详细Address(比如Google Map开发).等。而在Android 中通过LocationManager来获取Location.通常获取Location有GPS获取,WIFI获取。
我今天做一个简单的小Demo,来教大家如何获取Location,从而获取经纬度。下一节将教大家通过Location来获取Address.
首先第一步:
创建一个Android工程命名为LocationDemo.
第二步:修改main.xml代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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,代码如下:
- packagecom.android.tutor;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.Location;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassLocationDemoextendsActivity{
- privateTextViewlongitude;
- privateTextViewlatitude;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- longitude=(TextView)findViewById(R.id.longitude);
- latitude=(TextView)findViewById(R.id.latitude);
- LocationmLocation=getLocation(this);
- longitude.setText("Longitude:"+mLocation.getLongitude());
- latitude.setText("Latitude:"+mLocation.getLatitude());
- }
- //GettheLocationbyGPSorWIFI
- publicLocationgetLocation(Contextcontext){
- LocationManagerlocMan=(LocationManager)context
- .getSystemService(Context.LOCATION_SERVICE);
- Locationlocation=locMan
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(location==null){
- location=locMan
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- returnlocation;
- }
- }
第四步:增加权限,修改AndroidManifest.xml代码如下(第16行为所增行):
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.tutor"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".LocationDemo"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <uses-sdkandroid:minSdkVersion="7"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- </manifest>
第五步:运行LocationDemo工程,所得效果如下(真机深圳测试):
转自:http://blog.csdn.net/android_tutor/article/details/5672911
大家好,今天说说Location, Location在Android开发中还是经常用到的,比如 通过经纬度获取天气,根据Location获取所在地区详细Address(比如Google Map开发).等。而在Android 中通过LocationManager来获取Location.通常获取Location有GPS获取,WIFI获取。
我今天做一个简单的小Demo,来教大家如何获取Location,从而获取经纬度。下一节将教大家通过Location来获取Address.
首先第一步:
创建一个Android工程命名为LocationDemo.
第二步:修改main.xml代码如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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,代码如下:
- packagecom.android.tutor;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.location.Location;
- importandroid.location.LocationManager;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- publicclassLocationDemoextendsActivity{
- privateTextViewlongitude;
- privateTextViewlatitude;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- longitude=(TextView)findViewById(R.id.longitude);
- latitude=(TextView)findViewById(R.id.latitude);
- LocationmLocation=getLocation(this);
- longitude.setText("Longitude:"+mLocation.getLongitude());
- latitude.setText("Latitude:"+mLocation.getLatitude());
- }
- //GettheLocationbyGPSorWIFI
- publicLocationgetLocation(Contextcontext){
- LocationManagerlocMan=(LocationManager)context
- .getSystemService(Context.LOCATION_SERVICE);
- Locationlocation=locMan
- .getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if(location==null){
- location=locMan
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- returnlocation;
- }
- }
第四步:增加权限,修改AndroidManifest.xml代码如下(第16行为所增行):
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.tutor"
- android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".LocationDemo"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <uses-sdkandroid:minSdkVersion="7"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- </manifest>
第五步:运行LocationDemo工程,所得效果如下(真机深圳测试):
转自:http://blog.csdn.net/android_tutor/article/details/5672911