Android中怎么实现GPS定位

本篇文章为大家展示了Android中怎么实现GPS定位,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联专注于顺义企业网站建设,响应式网站开发,商城网站建设。顺义网站建设公司,为顺义等地区提供建站服务。全流程按需网站设计,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务

在AndroidManifest.xml中加入权限:


package com.example.tt;  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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;  public class MainActivity extends Activity {          @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                          Button button=(Button)findViewById(R.id.button1);         button.setOnClickListener(new OnClickListener() {                          @Override             public void onClick(View arg0) {                 // TODO Auto-generated method stub                 String serviceString=Context.LOCATION_SERVICE;                 LocationManager locationManager=(LocationManager)getSystemService(serviceString);                 String provider=LocationManager.GPS_PROVIDER;                 Location location=locationManager.getLastKnownLocation(provider);                 getLocationInfo(location);                                 locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);             }         });              }     private void getLocationInfo(Location location) {         String latLongInfo;         TextView lo=(TextView)findViewById(R.id.textView1);         if(location!=null){             double lat=location.getLatitude();             double lng=location.getLongitude();             latLongInfo="Lat:"+lat+"\nLong:"+lng;             lo.setText(latLongInfo);         }else {             latLongInfo="No location found";             lo.setText(latLongInfo);         }            }     private final LocationListener locationListener =new LocationListener() {                @Override         public void onStatusChanged(String provider, int status, Bundle extras) {             // TODO Auto-generated method stub                      }                @Override         public void onProviderEnabled(String provider) {             getLocationInfo(null);                      }                @Override         public void onProviderDisabled(String provider) {             getLocationInfo(null);                   }                @Override         public void onLocationChanged(Location location) {             getLocationInfo(location);             Toast.makeText(MainActivity.this, "位置改变了::::::::::::", 3000).show();         }     }; }

当需要使用基站定位时,可以将String provider=LocationManager.GPS_PROVIDER;改为**String provider=LocationManager.NETWORK_PROVIDER;
**
具体如果还要判断GPS搜索不到时切换基站定位,那样的功能就不要我写了,新手都应该会.
还有就是如果用到Google定位到哪个城市地点什么的,也easy了。

上述内容就是Android中怎么实现GPS定位,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站名称:Android中怎么实现GPS定位
分享链接:http://myzitong.com/article/piicje.html