Android获取GPS坐标:

Android获取GPS坐标:

1.package an.Android.application;  
2.  
3.  
4.import java.util.Iterator;  
5.  
6.import android.app.Activity;  
7.import android.content.Intent;  
8.  
9.import android.location.GpsSatellite;  
10.import android.location.GpsStatus;  
11.import android.location.Location;  
12.import android.location.LocationListener;  
13.import android.location.LocationManager;  
14.import android.os.Bundle;  
15.import android.util.Log;  
16.import android.view.Menu;  
17.import android.view.MenuItem;  
18.import android.view.View;  
19.import android.widget.Button;  
20.import android.widget.Toast;  
21.  
22.public class SpeedToll extends Activity {  
23.    /** Called when the activity is first created. */  
24.      
25.      
26.    private static final int    Search      = Menu.FIRST;  
27.    private static final int    Myloc       = Menu.FIRST + 1;  
28.    private static final int    Exit        = Menu.FIRST + 2;  
29.      
30.      
31.    private LocationManager locationManager;  
32.    private GpsStatus gpsstatus;  
33.      
34.      
35.    @Override  
36.    public void onCreate(Bundle savedInstanceState) {  
37.          
38.        super.onCreate(savedInstanceState);  
39.        /* 加载main.xml Layout */   
40.        setContentView(R.layout.desktop);  
41.        /* 以findViewById()取得Button对象,并加入onClickListener */  
42.        Button b1 = (Button) findViewById(R.id.button1);  
43.        b1.setOnClickListener(new Button.OnClickListener()  
44.        {   
45.          public void onClick(View v)   
46.          {  
47.            /* new一个Intent对象,并指定要启动的class */   
48.            Intent intent = new Intent();   
49.            intent.setClass(SpeedToll.this, Map.class);   
50.            /* 调用一个新的Activity */   
51.            startActivity(intent);  
52.            /* 关闭原本的Activity */   
53.            //SpeedToll.this.finish();   
54.            }   
55.          });  
56.          
57.          
58.        Button b2 = (Button) findViewById(R.id.button1);  
59.        b2.setOnClickListener(new Button.OnClickListener()  
60.        {   
61.          public void onClick(View v)   
62.          {  
63.            /* new一个Intent对象,并指定要启动的class */   
64.            Intent intent = new Intent();   
65.            intent.setClass(SpeedToll.this, Map.class);   
66.            /* 调用一个新的Activity */   
67.            startActivity(intent);  
68.            /* 关闭原本的Activity */   
69.            //SpeedToll.this.finish();   
70.            }   
71.          });  
72.    }   
73.      
74.    public boolean onCreateOptionsMenu(Menu menu) {  
75.        super.onCreateOptionsMenu(menu);  
76.        menu.add(0, Search, Menu.NONE, "搜索");  
77.        menu.add(0, Myloc, Menu.NONE, "定位");   
78.        menu.add(0, Exit, Menu.NONE, "退出");  
79.          
80.          
81.        return true;    
82.    }//menu   
83.   
84.    public boolean onOptionsItemSelected(MenuItem item)  
85.    {  
86.        //super.onOptionsItemSelected(item);   
87.        switch(item.getItemId()){  
88.            case Search:  
89.                //Search();   
90.                break;  
91.                  
92.            case Myloc:  
93.                GetMyLocation();  
94.                break;  
95.                  
96.            case Exit:  
97.                //mLocationManager.removeUpdates(this); //关闭GPS   
98.                  
99.                this.finish();  
100.                break;  
101.        }  
102.        return super.onOptionsItemSelected(item);  
103.    }  
104.      
105.      
106.    public boolean GetMyLocation(){  
107.        //获取到LocationManager对象   
108.        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);  
109.          
110.        //根据设置的Criteria对象,获取最符合此标准的provider对象   
111.        String currentProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();  
112.          
113.        //根据当前provider对象获取最后一次位置信息   
114.        Location currentLocation = locationManager.getLastKnownLocation(currentProvider);  
115.        //如果位置信息为null,则请求更新位置信息   
116.        if(currentLocation == null){  
117.            locationManager.requestLocationUpdates(currentProvider, 0, 0, locationListener);  
118.        }  
119.        //增加GPS状态监听器   
120.        locationManager.addGpsStatusListener(gpsListener);  
121.          
122.        //直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度   
123.        //每隔10秒获取一次位置信息   
124.        while(true){  
125.            currentLocation = locationManager.getLastKnownLocation(currentProvider);  
126.            if(currentLocation != null){  
127.                Log.d("Location", "Latitude: " + currentLocation.getLatitude());  
128.                Log.d("Location", "location: " + currentLocation.getLongitude());  
129.                Toast.makeText(SpeedToll.this, "Latitude: " + currentLocation.getLatitude()+ " "  
130.                        +"location: " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();  
131.                break;  
132.            }else{  
133.                Log.d("Location", "Latitude: " + 0);  
134.                Log.d("Location", "location: " + 0);  
135.            }  
136.            try {  
137.                Thread.sleep(10000);  
138.            } catch (InterruptedException e) {  
139.                 Log.e("Location", e.getMessage());  
140.            }  
141.        }  
142.        return false;  
143.     }  
144.       
145.     private GpsStatus.Listener gpsListener = new GpsStatus.Listener(){  
146.         //GPS状态发生变化时触发   
147.         @Override  
148.         public void onGpsStatusChanged(int event) {  
149.             //获取当前状态   
150.             gpsstatus=locationManager.getGpsStatus(null);  
151.             switch(event){  
152.                 //第一次定位时的事件   
153.                 case GpsStatus.GPS_EVENT_FIRST_FIX:  
154.                     break;  
155.                 //开始定位的事件   
156.                 case GpsStatus.GPS_EVENT_STARTED:  
157.                     break;  
158.                 //发送GPS卫星状态事件   
159.                 case GpsStatus.GPS_EVENT_SATELLITE_STATUS:  
160.                     Toast.makeText(SpeedToll.this, "GPS_EVENT_SATELLITE_STATUS", Toast.LENGTH_SHORT).show();  
161.                     Iterable<GpsSatellite> allSatellites = gpsstatus.getSatellites();     
162.                     Iterator<GpsSatellite> it=allSatellites.iterator();   
163.                     int count = 0;  
164.                     while(it.hasNext())     
165.                     {     
166.                         count++;  
167.                     }  
168.                     Toast.makeText(SpeedToll.this, "Satellite Count:" + count, Toast.LENGTH_SHORT).show();  
169.                     break;  
170.                 //停止定位事件   
171.                 case GpsStatus.GPS_EVENT_STOPPED:  
172.                     Log.d("Location", "GPS_EVENT_STOPPED");  
173.                     break;  
174.             }  
175.         }  
176.     };  
177.       
178.       
179.     //创建位置监听器   
180.     private LocationListener locationListener = new LocationListener(){  
181.         //位置发生改变时调用   
182.         @Override  
183.         public void onLocationChanged(Location location) {  
184.             Log.d("Location", "onLocationChanged");  
185.         }  
186.   
187.         //provider失效时调用   
188.         @Override  
189.         public void onProviderDisabled(String provider) {  
190.             Log.d("Location", "onProviderDisabled");  
191.         }  
192.   
193.         //provider启用时调用   
194.         @Override  
195.         public void onProviderEnabled(String provider) {  
196.             Log.d("Location", "onProviderEnabled");  
197.         }  
198.   
199.         //状态改变时调用   
200.         @Override  
201.         public void onStatusChanged(String provider, int status, Bundle extras) {  
202.             Log.d("Location", "onStatusChanged");  
203.         }  
204.           
205.          
206.    };  
207.      
208.} 
主要有两个文件,一个是主文件,一个是xml文件。

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值