Android 高手進階教程(十四)之—-Android Location的使用!!

來源:互聯網
上載者:User

大家好,今天說說Location
, Location
Android
開發中還是經常用到的,比如 通過經緯度擷取天氣,根據Location
擷取所在地區詳細Address
(比如Google Map
開發).等。而在Android
中通過LocationManager
來擷取Location
.通常擷取Location
GPS
擷取,WIFI
擷取。

我今天做一個簡單的小Demo
,來教大家如何擷取Location
,從而擷取經緯度。下一節將教大家通過Location
來擷取Address
.

 

首先第一步:

 

建立一個Android
工程命名為LocationDemo
.

 

第二步:修改main.xml
代碼如下:

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br />android:id="@+id/longitude"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="longitude:"<br /> /><br /><TextView<br />android:id="@+id/latitude"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:text="latitude:"<br /> /><br /></LinearLayout><br />

 

第三步:修改LocationDemo.java
,代碼如下:

package com.android.tutor;<br />import android.app.Activity;<br />import android.content.Context;<br />import android.location.Location;<br />import android.location.LocationManager;<br />import android.os.Bundle;<br />import android.widget.TextView;<br />public class LocationDemo extends Activity {</p><p>private TextView longitude;<br />private TextView latitude;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> longitude = (TextView)findViewById(R.id.longitude);<br /> latitude = (TextView)findViewById(R.id.latitude);</p><p> Location mLocation = getLocation(this);</p><p> longitude.setText("Longitude: " + mLocation.getLongitude());<br /> latitude.setText("Latitude: " + mLocation.getLatitude());<br /> }</p><p> //Get the Location by GPS or WIFI<br />public Location getLocation(Context context) {<br />LocationManager locMan = (LocationManager) context<br />.getSystemService(Context.LOCATION_SERVICE);<br />Location location = locMan<br />.getLastKnownLocation(LocationManager.GPS_PROVIDER);<br />if (location == null) {<br />location = locMan<br />.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);<br />}<br />return location;<br />}<br />}

 

第四步:增加許可權,修改AndroidManifest.xml
代碼如下(第16行為所增行):

<?xml version="1.0" encoding="utf-8"?><br /><manifest xmlns:android="http://schemas.android.com/apk/res/android"<br /> package="com.android.tutor"<br /> android:versionCode="1"<br /> android:versionName="1.0"><br /> <application android:icon="@drawable/icon" android:label="@string/app_name"><br /> <activity android:name=".LocationDemo"<br /> android:label="@string/app_name"><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> </application><br /> <uses-sdk android:minSdkVersion="7" /><br /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><br /></manifest>

 

第五步:運行LocationDemo
工程,所得效果如下(真機深圳測試):

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.