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;
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) {
//精度和緯度
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);
}
}