腾讯地图定位demo

来源:互联网 发布:淘宝网店如何起步 编辑:程序博客网 时间:2024/05/16 16:58

最近做了一个使用腾讯地图,使用定位功能的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

0 0
原创粉丝点击