1. 程式人生 > >、簡單實現百度地圖實時定位功能

、簡單實現百度地圖實時定位功能

首先是在百度地圖官網註冊賬戶,然後進行申請api,其他就不囉嗦了,獲取sha1碼方法

參考如下文章  http://jingyan.baidu.com/article/a681b0de0f860f3b184346bc.html

上程式碼

public class MainActivity extends AppCompatActivity {
    MapView mapView;
    // TODO: 2017-06-21 獲取實時定位
BaiduMap baiduMap;
    LocationManager locationmanger;
    private String provider;
    private boolean 
isFirstlocats = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //獲取sdk的值,進行初始化,呼叫getApplicationContext獲取全域性傳入 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mapView
= (MapView) findViewById(R.id.map_view); //獲取baidumap例項 baiduMap = mapView.getMap(); // TODO: 2017-06-21 修改實現本人位置顯示 baiduMap.setMyLocationEnabled(true); //獲取位置提供器 locationmanger = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //獲取所有位置提供器 //獲取所有可用的額位置提供器 List<String> providerlist = locationmanger
.getProviders(true); if (providerlist.contains(LocationManager.GPS_PROVIDER)) { provider = LocationManager.GPS_PROVIDER; } else if (providerlist.contains(LocationManager.NETWORK_PROVIDER)) { provider = LocationManager.NETWORK_PROVIDER; } else { //當前沒有可用的位置提供器 Toast.makeText(this, "當前沒有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Location location = locationmanger.getLastKnownLocation(provider); if (location != null) { //顯示當前裝置位置資訊 showlocation(location); } locationmanger.requestLocationUpdates(provider, 5000, 1, locationlistener); } LocationListener locationlistener = new LocationListener() { @Override public void onLocationChanged(Location location) { //更新當前位置資訊 if (location != null) { Log.e("jknhkhnj", "lkklmkl"); showlocation(location); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; private void showlocation(final Location locationmessage) { if (isFirstlocats) { // String currentposition = "經度 :" + locationmessage.getLatitude() + "\n" + "緯度 :" + locationmessage.getLongitude(); // Log.e("currentposition", "===" + currentposition); LatLng ll = new LatLng(locationmessage.getLatitude(), locationmessage.getLongitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(update); update = MapStatusUpdateFactory.zoomBy(16f); baiduMap.animateMapStatus(update); isFirstlocats = false; } // TODO: 2017-06-21 新增修改實現本人位置顯示 MyLocationData.Builder locationbuilder = new MyLocationData.Builder(); locationbuilder.latitude(locationmessage.getLatitude()); locationbuilder.longitude(locationmessage.getLongitude()); MyLocationData locationData = locationbuilder.build(); baiduMap.setMyLocationData(locationData); } @Override protected void onDestroy() { //activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命週期管理 super.onDestroy(); // TODO: 2017-06-21 增加顯示個人位置 baiduMap.setBaiduHeatMapEnabled(false); mapView.onDestroy(); if (locationmanger != null) { //關閉程式時候監聽器移除 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationmanger.removeUpdates(locationlistener); } } @Override protected void onPause() { super.onPause(); //activity執行onPause時執行mMapView. onPause (),實現地圖生命週期管理 mapView.onPause(); } @Override protected void onResume() { //activity執行onResume時執行mMapView. onResume (),實現地圖生命週期管理 super.onResume(); mapView.onResume(); } } -=================================================================== 需要給build.gradle中新增如下程式碼
/**
 * 目前 Android Studio不支援自動新增 .so檔案(Eclipse是支援的),所以我們需要手動載入libs下的.so的動態庫
*/
sourceSets {
    main() {
        jniLibs.srcDirs = ['libs']
    }
}
==============================================
許可權問題
//百度地圖的許可權
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
==================================================
注意
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    //百度地圖的設定
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="pPcy7cpMIxvofYKRiCItv6BpwrvTZG1s" />
</application>


相關推薦

簡單實現地圖實時定位功能

首先是在百度地圖官網註冊賬戶,然後進行申請api,其他就不囉嗦了,獲取sha1碼方法 參考如下文章  http://jingyan.baidu.com/article/a681b0de0f860f3b184346bc.html 上程式碼 public class Main

ionic使用地圖實時定位並匯入標記

最近遇到前端需要實現地圖的功能,其實呼叫地圖的例子網上有好多,這邊介紹一下我們如何通過ionic框架在前端呼叫百度地圖。 首先我們需要在src目錄下的index.html檔案中引入百度地圖,在這之前請申請百度地圖金鑰(金鑰在百度地圖官網申請) <script

地圖定位功能,註冊祕鑰

1.1     百度地圖定位GPS  + WIFI  +  基站1.     GPS定位:是美國軍方提供全球定位系統。5~15米        北斗:中國研發定位系統。目前最先進的定位系統,精度能達到幾釐米        格洛納斯:俄羅斯定位系統,已交於北斗維護。     

vue中實現地圖拖拽地圖定位功能

.com 元素 偏移量 locals 進行 函數 nco 查看 page 效果如果所示,拖動地圖。中間圖標不動,並且自動獲取地圖當前中心點的經緯度。然後就可以用經緯度做其他的操作了。。。首先查看了百度地圖的api。能實現這個功能最貼近的就是marker。marker

H5跳轉到地圖定位

獲取 targe mark appname xpl ofo div 忽略 視野 找了半天的JS api,發現沒有,後來發現這個叫 url api,讓我好找。 官方文檔: http://lbsyun.baidu.com/index.php?title=uri/api/web

地圖API定位+顯示位置

col mar 位置 百度地圖api fail true acc ntp 引入 1. 先在需要嵌入地圖的頁面引入map.js <script src="http://api.map.baidu.com/api?v=2.0&ak=你的秘鑰"></s

android地圖基礎定位不用開啟許可權也可以獲取經緯度成功?什麼鬼

最近專案中需要使用百度地圖sdk,發現一個問題,手機未開啟地圖定位許可權,在地圖定位監聽裡面竟然可以返回狀態碼為161的正確定位返回,是不是很神奇。 測試了百度demo,把定位許可權關閉,也是可以定位成功的。看來確實不需要定位許可權,就可以獲取定位資訊呀。 這就讓我暈了,那這個許可權什麼時候

Android地圖開發定位和路線導航

在寫之前要先加入百度地圖sdk的jar包從百度地圖開發者官網上就能下載 百度地圖sdk下載 官網(可翻牆選擇):http://developer.android.com/sdk/index.html 不可翻牆選擇:http://www.androiddevtools.cn/ 而其中用

php+js實現地圖多點標註的方法

本文例項講述了php+js實現百度地圖多點標註的方法。分享給大家供大家參考,具體如下: 1.php建立json資料 ? 1

地圖獲取定位 React Native

獲取金鑰:http://lbsyun.baidu.com/apiconsole/key 去百度地圖申請應用; 百度地圖api(ak值申請後可以獲得,安全碼在申請應用點選設定後可以檢視) BaiduMap_URL =‘https://api.map.baid

Vue:地圖API 定位 地點檢索

一個全棧向前端的退化過程。  (咳~抱怨一句)   高德--API清晰,就是定位可能差了那麼點! 百度--國內較好支援國外定位 搜尋的平臺。(不過需要申請配額) google -- 需要翻牆,沒花錢配額之前給你一次呼叫機會,   使用需求因個

地圖api定位,根據經緯度顯示當前城市名

當前城市:<span id="pro_num">鄭州</span> <div id="allmap"></div> <script type="text/javascript" src="http://api.map.baidu.com/

呼叫地圖API定位位置

以下程式碼是百度開發示例程式碼: <html> <head>     <meta http-equiv="Content-Type" content="text/html; charse

調用地圖API定位位置

nbsp stat toc tle over enable city user 密鑰 以下代碼是百度開發示例代碼: <html> <head> <meta http-equiv="Content-Type" content="text

地圖定位

百度地圖的展示,及詳細配置 官方Api 開始搭建環境 1.下載SDK 匯入Jar包,這裡分為2塊,第一塊是畫圈的jar包,需要小奶瓶,第二部分是檔案直接賦值即可 **清單檔案註冊** <application ..... <!

OC簡單使用地圖

1.使用pod獲取百度地圖SDK: pod 'BaiduMapKit' pod install 2.修改AppDelegate.m 為AppDelegate.mm,在AppDelegate.mm檔案中配置key: /**配置百度地圖 需引入標頭檔案(<B

JS方式實現地圖載入資料庫中的座標並實現動態重新整理

"); //設定地圖顯示的城市,這項是必須的map.enableScrollWheelZoom(true); //開啟滑鼠滾輪縮放var gPoints = [];Ext.Ajax.request({url : 'coords.do?method=getCoords',params : {},success

Android應用中使用地圖API定位自己的位置(二)

百度地圖SDK為開發者們提供瞭如下型別的地圖覆蓋物: 我的位置圖層(MyLocationOverlay):用於顯示使用者當前位置的圖層(支援自定義位置圖示); Poi搜尋結果圖層(PoiOverlay):用於顯示興趣點搜尋結果的圖層; 路線圖層(RouteOve

JQuery條件下使用 JS方式實現地圖載入資料庫中的座標並實現動態重新整理

功能:藉助JQuery,使用 JS 方式實現後臺載入座標資料然後顯示在百度地圖上,並每隔5秒重新整理一次資料。 對不同點使用不同的圖示進行標註,比如上傳時間大於半小時的點標記為紅色圖示,說明資料不實時, 對上傳時間短於半小時的,則用綠色的點表示正在運動的目標,藍色的點表示靜