GPS定位系统的介绍

1:主要实现的功能:

(1)实现用户的追踪,可以得到用户的经度和纬度

(2)得到本地可以提供定位服务的方式

(3)通过Criteria 条件的筛选,我们可以得到最好的,满足要求的定位服务


<LinearLayout 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:orientation="vertical" >


    <Button 
        android:id="@+id/locationButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定监听"
        />
    
     <Button 
        android:id="@+id/providerButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看本地的定位服务方式"
        />
     
      <Button 
        android:id="@+id/bestproviderButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择最好的提供定位的方式"
        />
</LinearLayout>

---------------------------------------------------------------------------------

package com.example.userlocation;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import po.UserLocation;


import android.support.v7.app.ActionBarActivity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


/*
 * 主要使用GPS定位的功能,得到用户的经度和纬度
 * (1)在AndroidManifest。xml文件中配置权限
 * (2)获取LocationManager
 * (3)选择LocationProvider
 * (4)绑定监听的事件
 * */
public class UserLocaltionActivity extends ActionBarActivity {




private Button locationButton;
private LocationManager locationManager;
//获取本地提供定位的方式
private Button providerButton;
//获取本地满足条件最好的定位服务方式
private Button bestProviderButton;

//追踪用户的位置
private List<UserLocation> list=new ArrayList<UserLocation>();




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_localtion);

locationButton =(Button)findViewById(R.id.locationButton);
providerButton =(Button)findViewById(R.id.providerButton);
bestProviderButton=(Button)findViewById(R.id.bestproviderButton);

locationButton.setOnClickListener(new LocationButtonListener());
providerButton.setOnClickListener(new ProviderLocation());
bestProviderButton.setOnClickListener(new BestProviderLocation());


}




class ProviderLocation implements OnClickListener{


@Override
public void onClick(View v) {
List<String> list=locationManager.getAllProviders();
for(String provider : list){
System.out.println(provider);
}
}

}



/*
* 这个按钮主要是选择本地机器上满足条件的定位方式
* */
class BestProviderLocation implements OnClickListener{


@Override
public void onClick(View v) {
Criteria certial=new Criteria();
certial.setAccuracy(Criteria.ACCURACY_HIGH);
certial.setAltitudeRequired(false);
certial.setCostAllowed(false);
String bestprovider=locationManager.getBestProvider(certial, true);
System.out.println(bestprovider);
}

}

/*
* 绑定位置变化的监听
* */
class LocationButtonListener implements OnClickListener{


@Override
public void onClick(View view) {
//获取LocationManager的对象
   locationManager=(LocationManager)UserLocaltionActivity.this.getSystemService(Context.LOCATION_SERVICE);
//当用户的位置发生变化的时候,该方法会被调用
/*
* 第一个参数:使用哪种定位的服务
* 第二个参数:多长时间更新位置
* 第三个参数:位置
* 第四个参数:创建监听的事件
* */
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, new GPSLocationListener());
}

}


/*
* 监听的类
* */
class GPSLocationListener implements LocationListener{


/*
* 当位置发生变化时,该方法被调用
* */
@Override
public void onLocationChanged(Location location) {
//精度和纬度
/*
* 使用List集合装载用户位置的变化

* */
UserLocation mylocation=new UserLocation();
mylocation.setLatitude(location.getLatitude());
mylocation.setLongation(location.getLongitude());
list.add(mylocation);
System.out.println(location.getLongitude());
System.out.println(location.getLatitude());
}


@Override
public void onProviderDisabled(String arg0) {

}


@Override
public void onProviderEnabled(String arg0) {

}


@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.user_localtion, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值