執行畫面

AndroidManifest.xml權限配置如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jrdgpst02"
    android:versionCode="1"
    android:versionName="1.0" > 
    <uses-sdk
        android:minSdkVersion="8"
     />
       <uses-feature android:name="android.hardware.location.gps"/>
    <!-- 權限設定 -->
     <uses-permission android:name="android.permission.INTERNET"></uses-permission>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.jrdgpst02.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>
</manifest>

<RelativeLayout xmlns:android="http://schemas.android.com/
apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="39dp"
        android:layout_marginTop="41dp"
        android:text="TextView" />
</RelativeLayout>


 程式碼
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.example.jrdgpst02;
import java.util.List;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity{
private LocationManager locationManager;
TextView t1;
GPSListener locationListenerNGPS ;
GPSListener locationListenerGPS ;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
geocoder = new Geocoder(this);
t1=(TextView)findViewById(R.id.textView1);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListenerNGPS = new GPSListener();
locationListenerGPS = new GPSListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNGPS); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGPS);
t1.setText("Try GPS!!\r\n");
}
class GPSListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
t1.append("location: lat:" + location.getLatitude() + ", long"+location.getLongitude() + "\r\n");
String strAddress = "";
List places = null;
try
{
places = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);
}catch(Exception e) {
e.printStackTrace();
}
if(places != null && places.size() > 0)
{
strAddress = ((Address)places.get(0)).getAddressLine(0)+ "," +
((Address)places.get(0)).getAddressLine(1)+","+
((Address)places.get(0)).getCountryName();
}
t1.append(strAddress+"\n");
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}



 Eclipse相關套件要先裝好,小提醒GPSGOOGLE MAP幾乎一年半就會改一次。
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()


    留言列表 留言列表

    發表留言