1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 关于百度地图定位获取当前位置 不获取地图mapview

关于百度地图定位获取当前位置 不获取地图mapview

时间:2021-12-18 06:37:48

相关推荐

关于百度地图定位获取当前位置 不获取地图mapview

最近做了关于地图定位的小demo,最后成功了,来完整的记录下,帮助跟我一样蒙了个逼的娃们.....

/u011068702/article/details/49181169这个博主写的非常好,我是参考他的,特此提出来出处

【1】到百度地图开放平台,创建应用,获取key值

-----选择Android SDK

----要填写发布版的SHA1和包名

----win+R,输入cmd,打开系统命令行

-----输入以下两句话:(1)cd .android (2)keytool -list -v -keystore debug.keystore

-----密码是android ,他是输入隐藏的,不要看着没反应就一直输,输完回车等一下就会有信息

------我们需要的SHA1值就在下面

----包名在manifests文件里,引号里面的

-----这两个写好后会自动生成一个key值

【2】下载官方SDK for Android的,获取jar包和so文件

-----这个文件不能放到这个上面来么....这个如果没下对或者什么的可以找我发给你

【3】进入Android studio,开始干活了

把下载好的jar包和so文件复制过来,具体操作,jar包放到libs下(jar包要导入的,导入以后才会有可以点开的小三角),在main文件下新建一个文件jinlibs,把其他的的文件夹放到这个里面,具体结构如下:

-----在APP的build gradle以下添加了jar包后的显示,导入了这个jar包,因为其他文件我们是新建了jinlibs,所以不用再导入之类的

-----在manifests里面加上数据信息和服务,这里的value后的就是我们已经成功申请到的key值,换成自己的:

<meta-dataandroid:name="com.baidu.lbsapi.API_KEY"android:value="Ysl8TgHTd3Fcr3h5CYGSjTTFhGIvdeuO" /><serviceandroid:name="com.baidu.location.f"android:enabled="true"android:process=":remote" ></service>

----他们的位置如下:

------加权限,这个是那个博主给出的,简洁方便!

<!-- 这个权限用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission><!-- 这个权限用于访问GPS定位--><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission><!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位--><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><!-- 获取运营商信息,用于支持提供运营商信息相关的接口--><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission><!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--><uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><!-- 用于读取手机当前的状态--><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><!-- 访问网络,网络定位需要上网--><uses-permission android:name="android.permission.INTERNET" /><!-- SD卡读取权限,用户写入离线定位数据--><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>

------写布局文件和activity,布局文件里是总的,和一个按钮显示位置,毕竟实际上我只要个位置

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.administrator.demo.MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="地理位置"android:textSize="20sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.446"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.012" /><TextViewandroid:id="@+id/textView"android:layout_width="279dp"android:layout_height="375dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.152" /><TextViewandroid:id="@+id/textView1"android:layout_width="279dp"android:layout_height="62dp"android:textColor="#F87461"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintHorizontal_bias="0.445"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.815" /><Buttonandroid:id="@+id/button"android:layout_width="130dp"android:layout_height="59dp"android:onClick="show_location"android:text="baidu"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.933"tools:ignore="OnClick" /></android.support.constraint.ConstraintLayout>

-------活动里面的代码之前博主写的非常简洁明了,也够用了

package com.example.administrator.demo;import android.Manifest;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.support.v4.app.ActivityCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.Poi;import java.util.List;public class MainActivity extends AppCompatActivity {public LocationClient mLocationClient = null;public BDLocationListener myListener = new MyLocationListener();private String addr;private String describe;private TextView map;private TextView mapp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startLocate();}/** 定位*/private void startLocate() {mLocationClient = new LocationClient(getApplicationContext());//声明LocationClient类mLocationClient.registerLocationListener(myListener); //注册监听函数LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系int span = 1000;option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要option.setOpenGps(true);//可选,默认false,设置是否使用gpsoption.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要mLocationClient.setLocOption(option);//开启定位mLocationClient.start();}private class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {StringBuffer sb = new StringBuffer(256);sb.append("time : ");sb.append(location.getTime());sb.append("\nerror code : ");sb.append(location.getLocType());sb.append("\nlatitude : ");sb.append(location.getLatitude());sb.append("\nlontitude : ");sb.append(location.getLongitude());sb.append("\nradius : ");sb.append(location.getRadius());if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果sb.append("\nspeed : ");sb.append(location.getSpeed());// 单位:公里每小时sb.append("\nsatellite : ");sb.append(location.getSatelliteNumber());sb.append("\nheight : ");sb.append(location.getAltitude());// 单位:米sb.append("\ndirection : ");sb.append(location.getDirection());// 单位度sb.append("\naddr : ");sb.append(location.getAddrStr());sb.append("\ndescribe : ");sb.append("gps定位成功");} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果sb.append("\naddr : ");sb.append(location.getAddrStr());addr=location.getAddrStr();//运营商信息sb.append("\noperationers : ");sb.append(location.getOperators());sb.append("\ndescribe : ");sb.append("网络定位成功");} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果sb.append("\ndescribe : ");sb.append("离线定位成功,离线定位结果也是有效的");} else if (location.getLocType() == BDLocation.TypeServerError) {sb.append("\ndescribe : ");sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@,会有人追查原因");} else if (location.getLocType() == BDLocation.TypeNetWorkException) {sb.append("\ndescribe : ");sb.append("网络不同导致定位失败,请检查网络是否通畅");} else if (location.getLocType() == BDLocation.TypeCriteriaException) {sb.append("\ndescribe : ");sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");}sb.append("\nlocationdescribe : ");sb.append(location.getLocationDescribe());// 位置语义化信息describe=location.getLocationDescribe();List<Poi> list = location.getPoiList();// POI数据if (list != null) {sb.append("\npoilist size = : ");sb.append(list.size());for (Poi p : list) {sb.append("\npoi= : ");sb.append(p.getId() + " " + p.getName() + " " + p.getRank());}}Log.e("描述:", sb.toString());map=findViewById(R.id.textView);mapp=findViewById(R.id.textView1);map.setText(sb.toString());}}public void show_location(View view){mapp.setText(addr+","+ describe);}}

【4】结果

在Android studio的logcat里显示的:

手机上显示的:

总结遇到的几个问题:

(1)SDK下载的有问题,这个下载的时候一定要看清哈,要不下的demo里面也应该会有

(2)jar包导入和so文件的安置问题.....之前我是都放在lib下了,后来改成这样我觉得更好一点

(3)key值,这个一定要和自己的包名相一致

(4)我是都配置好写好以后,编译也没有错误,但是不出东西,当时我的error code是162,传说中的错误代码.....后来是不停的编译,绝望中编啊编啊编啊,突然logcat就开始显示位置了。我敲里吗的........所以如果大家弄的都没有问题可以等一会,多编译几次,可能是他反应慢咋的......

(5)下载到真机以后遇到在电脑这边显示位置,一出去开流量就不行,显示什么获取位置失败一般是由飞行模式导致的巴拉巴拉....这个很有可能是我们的定位服务没有打开,即使申请了权限,但是不知道为撒子没有开,在手机里给他位置服务的权限就ojbk啦!

【 再一次向我参考借鉴的各位博主大佬比心!】

撒花✿✿ヽ(°▽°)ノ✿

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。