最近做了一個使用騰訊地圖,使用定位功能的demo。
功能實現:自動定位顯示經緯度,設置marker點,長按地圖顯示經緯度並設置marker。
準備工作申請key,下載jar包等操作,騰訊地圖的開發者平臺說的很清楚,不用多說
MainActivity佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.wjx.lbsdemo.MainActivity">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默認:" />
<!--經度-->
<EditText
android:id="@+id/edit_lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<!--經度-->
<EditText
android:id="@+id/edit_lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手動:" />
<!--經度-->
<EditText
android:id="@+id/ll_2_edit_lon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="經度"/>
<!--經度-->
<EditText
android:id="@+id/ll_2_edit_lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="緯度"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll_2">
<com.tencent.tencentmap.mapsdk.map.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/textview_shape"
android:text="(手動)長按地圖獲得所選點的經緯度"
android:textSize="18sp"
android:gravity="center_vertical"
/>
</RelativeLayout>
</RelativeLayout>
首先實現自動定位功能
一:創建定位監聽,也可以通過實現。
TencentLocationListener listener = new TencentLocationListener() {
@Override
public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
if (i == TencentLocation.ERROR_OK) {
//定位成功
} else {
Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onStatusUpdate(String s, int i, String s1) {
}
};
二:創建定位請求
TencentLocationRequest request = TencentLocationRequest.create();
request.setRequestLevel(1);
request.setInterval(60000);//每分鐘重新定位一次
TencentLocationManager locationManager = TencentLocationManager.getInstance(this);
int i = locationManager.requestLocationUpdates(request, listener);
這樣一個沒60s定位一次的功能即可實現,在定位成功的監聽中添加代碼,顯示經緯度,以及設置marker。
lon1,lat1爲兩個editText,顯示經緯度
TencentLocationListener listener = new TencentLocationListener() {
@Override
public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
if (i == TencentLocation.ERROR_OK) {
//定位成功
lon1.setText(tencentLocation.getLongitude() + "");
lat1.setText(tencentLocation.getLatitude() + "");
LatLng latLng = new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude());
Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.ic_launcher);
marker2 = mTencentMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory
.fromBitmap(bitmap))
.draggable(true));
animateTo(tencentLocation);
} else {
Toast.makeText(MainActivity.this, "失敗", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onStatusUpdate(String s, int i, String s1) {
}
};
··· ···
//animateTo()方法,功能爲將定位的座標作爲地圖的中心點顯示,mapview爲騰訊地圖
private void animateTo(TencentLocation location) {
if (location == null) {
return;
}
mapView.getController().animateTo(Utils.of(location));
mapView.getController().setCenter(Utils.of(location));
}
實現長按地圖設置marker點,並顯示經緯度功能。lon2,lat2爲兩個Edittext,顯示經緯度
MapView mapView = (MapView) findViewById(R.id.map);
TencentMap mTencentMap = mapView.getMap();
mTencentMap.setZoom(12);//設置定圖顯示等級
mTencentMap.setOnMapLongClickListener(new TencentMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng latLng) {
lon2.setText(latLng.getLongitude() + "");
lat2.setText(latLng.getLatitude() + "");
if (marker!=null) {
marker.remove();
}
marker = mTencentMap.addMarker(new MarkerOptions().position(latLng).icon(
defaultMarker())
.draggable(true));
}
});
效果顯示定位:
demo下載地址:
http://download.csdn.net/detail/qq_33474233/9834370