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:
注:这里不需要任何权限。
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;
}
}
}
答:
粗略定位和精确定位;
粗略定位主要是基于移动网网络 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;
}
}
}
相关文章推荐
- Android笔记三十二.Android位置服务及核心API
- 【Android】18.2 利用百度定位服务API实现位置跟踪
- 在android用Google Geocoding API服务解析地址(关键词:android/GPS/Geocoding API/getLocationFrom()返回null)
- 【代码】android通过criteria选择合适的地理位置服务
- 突袭HTML5之Javascript API扩展2―地理信息服务及地理位置API学习
- Android呼叫管理服务之会话发起协议(SIP)API
- Android窗口管理服务WindowManagerService计算窗口Z轴位置的过程分析
- android位置服务
- Android百度地图2.0运行定位到当前位置时“服务没有启动”
- android 默认打开位置服务,默认关闭GPS设置
- Android使用Google Map服务时获取Map API Key时在Win7下出现的问题
- 令人激动的Android位置相关API
- ANDROID基于位置的服务
- Android中帐户服务部+位置服务
- 百度map api for Android~搜索服务
- Android LBS系列04 位置和地图服务简介
- 在android用Google Geocoding API服务解析地址
- 【Based Android】android通过criteria选择合适的地理位置服务
- Android2.2调用Google Map api进行位置搜索
- Android窗口管理服务WindowManagerService计算窗口Z轴位置的过程分析