สำนักวิทยบริการและเทคโนโลยีสารสนเทศ (สวส.)

Office of Academic Resources and Information Technology


Office of Academic Resources and Information Technology

การดึงพิกัด GPS จากมือถือ Android เบื้องต้น

 


เมื่อ 4-5 ปีที่แล้ว ก่อนยุค SmartPhone จะออกสู่ตลาด ผู้เขียนเองสนใจศึกษาเทคโนโลยี
ทางด้าน GIS ซึ่งจำเป็นต้องใช้เครื่อง GPS Handheld ในการเก็บบันทึกพิกัดในภาคสนาม
ตอนนั้นจำได้ว่า ใช้เครื่องยี่ห้อ Garmin ราคา 7 พันกว่าๆ หน้าจอเป็นขาวดำ
ตอนลงพื้นที่เก็บข้อมูล จำเป็นต้องมีดินสอกปากา และสมุด เพื่อจดบันทึก
ซึ่งไม่สะดวกเอาซะเลย แต่มายุคนี้ โทรศัพท์มือถือส่วนใหญ่จะใส่ออพชั่น GPS มาให้ด้วย
ผู้เขียนคิดว่าออพชั่นนี้มีประโยชน์มากๆ โดยเฉพาะเจ้ามือถือ android เราสามารถ
ที่จะเขียนสั่งงาน GPS ได้อย่างสะดวกสบาย สามารถที่จะบันทึก
ข้อมูลพิกัด GPS และข้อมูลอ้างอิงของพิกัดนั้นๆได้อย่างง่ายดายเลยทีเดียว
บทความนี้จะอธิบายการเขียน Android Application
เพื่อดึงพิกัด GPS มาใช้ประโยชน์ โดยมีขั้นตอนดังนี้
1) เปิด Eclipse ไปที่ File – New Android Project ตั้งชื่อ ProjectName=GPSrecord ,PackageName=com.Example,Activity=Main
2) แก้ไขไฟล์ AndroidManifest.xml ดังนี้
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
    package="com.Example"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Main"
            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>
3)ไฟล์ res/layout/main.xml
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GPS Recorder" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lat" />
        <EditText
            android:id="@+id/txt_lat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lon" />
        <EditText
            android:id="@+id/txt_lon"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_start_gps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="     Start GPS     " />
        <Button
            android:id="@+id/btn_stop_gps"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="     Stop GPS     " />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|center_vertical"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="        Save            " />
    </LinearLayout>
</LinearLayout>
4)ไฟล์ Main.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.Example;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity implements View.OnClickListener{
    LocationManager myLocationManager;
    LocationListener myLocationListener;
    Button btn_start_gps,btn_save,btn_stop_gps;
    EditText txt_lat,txt_lon;
    String latt,lonn;
     
    public void initwidget(){
        btn_start_gps = (Button)findViewById(R.id.btn_start_gps);
        btn_start_gps.setOnClickListener(this);
        btn_save= (Button)findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
        btn_stop_gps = (Button)findViewById(R.id.btn_stop_gps);
        btn_stop_gps.setOnClickListener(this);     
        txt_lat = (EditText)findViewById(R.id.txt_lat);
        txt_lon = (EditText)findViewById(R.id.txt_lon);
    }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initwidget();   
        myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        myLocationListener = new MyLocationListener();
    }///////////// end onCreate ////////////////
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_start_gps:
                if (!isGpsEnable()){               
                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which){
                            case DialogInterface.BUTTON_POSITIVE:
                                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(intent);
                                break; 
                            case DialogInterface.BUTTON_NEGATIVE:
                                //No button clicked
                                break;
                            }
                        }
                    }; 
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Setting GPS?").setPositiveButton("Yes", dialogClickListener)
                        .setNegativeButton("No", dialogClickListener).show();              
                }else{                 
                     
                    myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,myLocationListener);
                }              
                break;
            case R.id.btn_stop_gps:
                myLocationManager.removeUpdates(myLocationListener);               
                txt_lat.setText("");
                txt_lon.setText("");               
                break;  
            case R.id.btn_save:
                if(txt_lat.getText().toString().equals("") || txt_lon.getText().equals("")){
                    return;
                }
                String lt = txt_lat.getText().toString();
                String ln = txt_lon.getText().toString();
                Toast.makeText(getApplicationContext(),"บันทึก "+lt+","+ln+" ลงฐานข้อมูล", Toast.LENGTH_SHORT).show();
                break;             
            }
    }   
    public boolean isGpsEnable(){ // เมธอดตรวจสอบว่าเปิด GPS หรือไม่
        boolean isgpsenable = false;
        String provider = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
                   if(!provider.equals("")){ //GPS is Enabled
                       isgpsenable = true;                
                   }       
        return isgpsenable;
    }   
    //class ใช้งาน GPS
    private class MyLocationListener implements LocationListener{
        public void onLocationChanged(Location argLocation) {
            latt = String.valueOf(argLocation.getLatitude());
            lonn = String.valueOf(argLocation.getLongitude());
            txt_lat.setText(latt);
            txt_lon.setText(lonn);
        }
        public void onProviderDisabled(String provider) {   }
        public void onProviderEnabled(String provider) {    }
        public void onStatusChanged(String provider,int status, Bundle extras) {    }
    };  ///  end Location class   
     
}/////////////////////end Main Class///////////
เมื่อรันโปรแกรม และกด Start GPS โปรแกรมจะให้เราเปิดฟังก์ชั่น GPS ของตัวเครื่องก่อน(หากปิดไว้)
ปล.ถ้าท่านรันบน emulator ให้กดปุ่ม send ที่ location control ของ emulater ก่อน ตามรูป
 
Credit : http://android4health.wordpress.com/2012/07/02/gps_android/