当前位置:   article > 正文

Android定位当前城市的实现_android 城市定位

android 城市定位

http://download.csdn.net/detail/u013091087/9405004

以下是一个定位获取当前所在城市的Android·Demo,用真机并且联网才能获取,不然就是奔溃报错,退出。

1、首先拿到相关的jar放到项目的libs中

2、在项目的AndroidManifest清单中添加一个定位服务和相关的权限

  1. <service
  2. android:name="com.baidu.location.f"
  3. android:enabled="true"
  4. android:process=":remote" >
  5. <intent-filter>
  6. <action android:name="com.baidu.location.service_v2.2" >
  7. </action>
  8. </intent-filter>
  9. </service>
  1. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  2. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  3. <uses-permission android:name="android.permission.INTERNET" />
  4. <uses-permission android:name="android.permission.READ_CONTACTS" />
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  6. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  7. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  8. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  9. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  10. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  11. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  12. <uses-permission android:name="android.permission.READ_LOGS" />
  13. <uses-permission android:name="android.permission.VIBRATE" />
  14. <uses-permission android:name="android.permission.WAKE_LOCK" />

3、开撸代码

1)、 布局文件activty_main:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <Button
  6. android:id="@+id/btn_location"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="定位" />
  10. <TextView
  11. android:id="@+id/tv_city"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_below="@id/btn_location"
  15. android:layout_marginTop="10dp"
  16. android:gravity="center"
  17. android:text="@string/hello_world" />
  18. </RelativeLayout>


2)、MyApplication,在AndroidManifest中的application中要注册上去,当打开app时,首先加载该类

该类具体是实现定位功能(发起定位,停止定位,定位相关属性设置,定位成功发送广播到MainActivity中进行处理)

  1. package com.jiaui.locationdemo;
  2. import android.app.Application;
  3. import android.content.Intent;
  4. import com.baidu.location.BDLocation;
  5. import com.baidu.location.BDLocationListener;
  6. import com.baidu.location.LocationClient;
  7. import com.baidu.location.LocationClientOption;
  8. public class MyApplication extends Application {
  9. private static LocationClient mLC = null;
  10. @Override
  11. public void onCreate() {
  12. // TODO Auto-generated method stub
  13. super.onCreate();
  14. initLocation();
  15. }
  16. /**
  17. * 初始化定位的内容
  18. */
  19. private void initLocation() {
  20. mLC = new LocationClient(this);
  21. // 使用eclipse的SHA1值和该Project的包名去百度地图API那里注册一个key
  22. mLC.setAK("Aiy12UcER9wwkppqaINQZoA0");
  23. mLC.registerLocationListener(new MyLocationListener());
  24. }
  25. /**
  26. * 发起定位
  27. */
  28. public static void requestLocationInfo() {
  29. setLocationOption();
  30. if (mLC != null && !mLC.isStarted())
  31. mLC.start();
  32. if (mLC != null && mLC.isStarted())
  33. mLC.requestLocation();
  34. }
  35. /**
  36. * 停止定位
  37. */
  38. private void stopLocationClient() {
  39. if (mLC != null && mLC.isStarted())
  40. mLC.stop();
  41. }
  42. /**
  43. * 设置定位相关参数
  44. */
  45. private static void setLocationOption() {
  46. LocationClientOption option = new LocationClientOption();
  47. option.setOpenGps(true);// 打开GPS
  48. option.setCoorType("bd09ll");// 设置坐标类型
  49. option.setServiceName("com.baidu.location.service_v2.9");
  50. option.setPoiExtraInfo(true);
  51. option.setAddrType("all");// 详细地址
  52. option.setPoiNumber(10);
  53. option.disableCache(true);
  54. option.setPriority(LocationClientOption.NetWorkFirst);
  55. option.setPriority(LocationClientOption.GpsFirst); // gps
  56. mLC.setLocOption(option);
  57. }
  58. private class MyLocationListener implements BDLocationListener {
  59. public void onReceiveLocation(BDLocation location) {
  60. if (location == null) {
  61. sendBroadCast("定位失败");
  62. return;
  63. }
  64. sendBroadCast(location.getCity());
  65. }
  66. public void onReceivePoi(BDLocation poilocation) {
  67. if (poilocation == null) {
  68. sendBroadCast("定位失败");
  69. return;
  70. }
  71. sendBroadCast(poilocation.getCity());
  72. }
  73. }
  74. /**
  75. * 得到结果发送广播
  76. *
  77. * @param city
  78. */
  79. private void sendBroadCast(String city) {
  80. stopLocationClient();// 停止定位
  81. Intent intent = new Intent(MainActivity.LOCATION_BCR);
  82. intent.putExtra("city", city);
  83. sendBroadcast(intent);
  84. }
  85. }


3)、MainActivity,注册广播-初始化组件-监听,操作。

  1. package com.jiaui.locationdemo;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. public class MainActivity extends Activity {
  12. private Button mLocationBtn;// 定位按钮
  13. private TextView mCityNameTv;// 显示当前定位成功的城市
  14. public static final String LOCATION_BCR = "location_bcr";
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. registerBroadCastReceiver();
  20. initView();
  21. initListen();
  22. }
  23. /**
  24. * 初始化组件
  25. */
  26. private void initView() {
  27. mLocationBtn = (Button) findViewById(R.id.btn_location);
  28. mCityNameTv = (TextView) findViewById(R.id.tv_city);
  29. }
  30. /**
  31. * 初始化监听
  32. */
  33. private void initListen() {
  34. mLocationBtn.setOnClickListener(new View.OnClickListener() {
  35. public void onClick(View v) {
  36. mCityNameTv.setText("定位中...");
  37. // 发起定位
  38. MyApplication.requestLocationInfo();
  39. }
  40. });
  41. }
  42. /**
  43. * 初始化广播,重写onReceive,处理接收回来的结果
  44. */
  45. private BroadcastReceiver mLocationBcr = new BroadcastReceiver() {
  46. public void onReceive(Context context, Intent intent) {
  47. String city = intent.getStringExtra("city");
  48. mCityNameTv.setText(city);
  49. }
  50. };
  51. /**
  52. * 注册广播
  53. */
  54. private void registerBroadCastReceiver() {
  55. IntentFilter intentToReceiverFilter = new IntentFilter();
  56. intentToReceiverFilter.addAction(LOCATION_BCR);
  57. registerReceiver(mLocationBcr, intentToReceiverFilter);
  58. }
  59. @Override
  60. protected void onDestroy() {
  61. // TODO Auto-generated method stub
  62. super.onDestroy();
  63. unregisterReceiver(mLocationBcr);// 注销广播
  64. }
  65. }

其中需要给项目申请一个百度key,具体操作如下图:



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/290456
推荐阅读
相关标签
  

闽ICP备14008679号