您的位置:首页 > 移动开发 > Android开发

android 位置服务 api

2014-05-20 10:27 197 查看
1、位置服务分几种?

答:

粗略定位和精确定位;

粗略定位主要是基于移动网网络 wfif,精确定位基于GPS。

如何在Android中使用GPS功能?

答:核心类,LocationManager,Location和LocationProvider。

获取GPS定位信息的通用步骤?

答:

1、获取系统的LocationManager对象

2、使用LocationManager,指定LocationProvider来获取定位信息,定位信息由Location表示

3、从Location对象中获取定位信息

demo:

//位置管理器
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//可以用的的LocationProvider的名字
List<String> names = locationManager.getAllProviders();

for (String name : names) {
Log.i("LOCATION", name);
System.out.println(names);
}

注:这里不需要任何权限。

NETWORK_PROVIDER,网络定位,包括利用基站和Wifi信号的定位方式;

PASSIVE_PROVIDER,被动,就是被动地等待其他应用更新位置;

GPS_PROVIDER,GPS定位,最靠谱的一种

如何获得符合指定条件的LocationProvider?

答:Criteria(条件,标准)

//位置管理器
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//创建一个LocationProvider的过滤条件
Criteria criteria=new Criteria();
criteria.setCostAllowed(true);//免费
//criteria.setAltitudeRequired(true);//提供高度信息
//criteria.setBearingRequired(true);//方向
List<String> names=locationManager.getProviders(criteria, false);
for (String name : names) {
Log.i("LOCATION", name);
}

如何获取定位信息?
答:

需要权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>java代码:
package com.example.gpsdemo;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

//位置管理器
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//位置更新监听器
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8,new LocationListener(){

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.i("LOCATION", location.getLatitude()+"-0");
Log.i("LOCATION", location.getLongitude()+"-1");
}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: