android百度地圖定位開發

來源:互聯網
上載者:User

標籤:

一、activity

import android.app.Activity;

import android.graphics.Point;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Message;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.LatLng;

/**
* 此demo用來展示如何結合定位SDK實現定位,並使用MyLocationOverlay繪製定位位置 同時展示如何使用自訂表徵圖繪製並點擊時彈出泡泡
*
*/
public class LocationDemo extends Activity {

// 定位相關
LocationClient mLocClient; //---------------first聲明
MapView mMapView;
BaiduMap mBaiduMap;
private LocationMode mCurrentMode;
public MyLocationListenner myListener = new MyLocationListenner();
MyLocationData locData ;
BitmapDescriptor mCurrentMarker;
boolean isFirstLoc = true;// 是否首次定位

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
//requestLocButton.setText("普通");
mCurrentMode = LocationMode.FOLLOWING;
// 1 地圖初始化
mMapView = (MapView) findViewById(R.id.bmapView);//經度120.219375,緯度30.259244 杭州
//mMapView = new MapView(this);

//mMapView.refreshDrawableState();
System.out.println("初始化成功啦,哈哈!");
mBaiduMap = mMapView.getMap();
// 2 開啟定位元影像層
mBaiduMap.setMyLocationEnabled(true);
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));

// 3 定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(myListener); //---------------first註冊定位監聽
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 開啟gps
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setCoorType("bd09ll"); // 設定座標類型
option.setScanSpan(3000);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option); //---------------first設定locationoption
mLocClient.start(); //---------------first啟動sdk定位
mLocClient.requestLocation();
}

/**
* 定位SDK監聽函數
*/
public class MyLocationListenner implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
// map view 銷毀後不在處理新接收的位置
if (location == null || mMapView == null)
return;
locData= new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此處設定開發人員擷取到的方向資訊,順時針0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();

System.out.println("location.lat"+location.getLatitude()+"&&&&&&&&"+"location.long"+location.getLongitude());

mBaiduMap.setMyLocationEnabled(true); //------1允許定位元影像層
mBaiduMap.setMyLocationData(locData); //------2允許定位元影像層後在設定定位元據
LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,19);
mBaiduMap.animateMapStatus(u);

if (isFirstLoc) {
isFirstLoc = false;
// 設定地圖縮放比例:17級100米

MapStatusUpdate ms = MapStatusUpdateFactory.newLatLngZoom(ll,19);
System.out.println("33333333333333333");
mBaiduMap.setMapStatus(ms);
}
}
}

@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}

@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}

@Override
protected void onDestroy() {
mLocClient.unRegisterLocationListener(myListener);
// 退出時銷毀定位
mLocClient.stop();
// 關閉定位元影像層
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;

super.onDestroy();
}

}

二、layout.xml (activity_location.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dip"
android:background="#D000"
android:minWidth="100dip"
android:orientation="vertical"
android:padding="2dp" >

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="定位icon" >

<RadioButton
android:id="@+id/defaulticon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="預設表徵圖" >
</RadioButton>

<RadioButton
android:id="@+id/customicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自訂表徵圖" >
</RadioButton>
</RadioGroup>
</LinearLayout>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="10dip" />

</RelativeLayout>

最後在設定檔中別忘了添加這個類。

 

android百度地圖定位開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.