Andorid 集成高德地图显示,定位

来源:互联网 发布:厦门唯一网络销售招聘 编辑:程序博客网 时间:2024/06/05 09:30

多处参考官方文档:http://lbs.amap.com/api/android-location-sdk/code-samples

使用真机测试,模拟器无法定位

效果图:

1.首先是权限:

<uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />    <uses-permission android:name="android.permission.WAKE_LOCK" />         <uses-permission android:name="android.permission.WRITE_SETTINGS" />    <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>            android:configChanges="orientation|keyboardHidden|screenSize"        </activity>        <!--记得写入keystore-->        <meta-data        android:name="com.amap.api.v2.apikey"        android:value="从高德官网的到的key值" />        <!-- 定位需要的服务 使用2.0的定位需要加上这个 -->        <service android:name="com.amap.api.location.APSService"></service>    </application>

具体获取key值的方法可以参照官网的文档

:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key

复制key的地方:


2.布局文件

<?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="text.bwie.com.xuejian1508a20171017.MainActivity">    <com.amap.api.maps2d.MapView        android:id="@+id/map"        android:layout_width="match_parent"        android:layout_height="match_parent" />//选择地图语言    <RadioGroup        android:layout_marginLeft="50dp"        android:id="@+id/check_language"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/border_bg"        android:paddingRight="4dp"        android:orientation="vertical">        <RadioButton            android:id="@+id/radio_ch"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="12sp"            android:checked="true"            android:text="中文地图"/>        <RadioButton            android:id="@+id/radio_en"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="12sp"            android:text="英文地图"/>    </RadioGroup>//选择地图模式    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_toRightOf="@id/check_language"        android:layout_margin="10dp"        android:layout_alignTop="@id/map">        <Button            android:id="@+id/basicmap"            android:layout_width="80dp"            android:layout_height="40dp"            android:gravity="center"            android:background="@drawable/btn_map_poi"            android:textColor="#000"            android:text="标准地图"/>        <Button            android:id="@+id/rsmap"            android:layout_width="80dp"            android:layout_height="40dp"            android:gravity="center"            android:background="@drawable/btn_map_poi"            android:textColor="#000"            android:text="卫星地图"/>    </LinearLayout></RelativeLayout>

3.导包:导入从高德官网下载的jar包


4. MainActicity的代码(此处的包名必须和在高德官网设置应用时的包名相同)

package xxx.xxx.xxx;import android.graphics.Color;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.RadioGroup;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps2d.AMap;import com.amap.api.maps2d.CameraUpdateFactory;import com.amap.api.maps2d.LocationSource;import com.amap.api.maps2d.MapView;import com.amap.api.maps2d.UiSettings;import com.amap.api.maps2d.model.BitmapDescriptorFactory;import com.amap.api.maps2d.model.MyLocationStyle;public class MainActivity extends AppCompatActivity implements View.OnClickListener, LocationSource, AMapLocationListener {    private OnLocationChangedListener mListener;    private AMapLocationClient mlocationClient;    private AMapLocationClientOption mLocationOption;    private MapView mapView;    private AMap aMap;    private Button basicmap;    private Button rsmap;    private RadioGroup mRadioGroup;    private UiSettings uiSettings;    public MainActivity() {    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//        requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏        mapView = (MapView) findViewById(R.id.map);        mapView.onCreate(savedInstanceState);// 此方法必须重写        init();    }    private void init() {        if (aMap == null) {            aMap = mapView.getMap();            setUpMap();        }        basicmap = (Button)findViewById(R.id.basicmap);        basicmap.setOnClickListener(this);        rsmap = (Button)findViewById(R.id.rsmap);        rsmap.setOnClickListener(this);        mRadioGroup = (RadioGroup) findViewById(R.id.check_language);        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                if (checkedId == R.id.radio_en) {                    aMap.setMapLanguage(AMap.ENGLISH);                } else {                    aMap.setMapLanguage(AMap.CHINESE);                }            }        });    }    /*设置一些amap的属性*/    private void setUpMap() {        // 自定义系统定位小蓝点        MyLocationStyle myLocationStyle = new MyLocationStyle();        myLocationStyle.myLocationIcon(BitmapDescriptorFactory                .fromResource(R.drawable.location_marker));// 设置小蓝点的图标        myLocationStyle.strokeColor(Color.BLACK);// 设置圆形的边框颜色        myLocationStyle.radiusFillColor(Color.argb(119,78, 137, 240));// 设置圆形的填充颜色        // myLocationStyle.anchor(int,int)//设置小蓝点的锚点        myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细        aMap.setMyLocationStyle(myLocationStyle);        aMap.setLocationSource(this);// 设置定位监听        uiSettings = aMap.getUiSettings();//获取ui设置对象        uiSettings.setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示        uiSettings.setScaleControlsEnabled(true);//设置显示地图的默认比例尺        uiSettings.setCompassEnabled(true);//显示指南针        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false    }    /**     * 方法必须重写     */    @Override    protected void onResume() {        super.onResume();        mapView.onResume();    }    /**     * 方法必须重写     */    @Override    protected void onPause() {        super.onPause();        mapView.onPause();        deactivate();    }    /**     * 方法必须重写     */    @Override    protected void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        mapView.onSaveInstanceState(outState);    }    /**     * 方法必须重写     */    @Override    protected void onDestroy() {        super.onDestroy();        mapView.onDestroy();    }    /*选择地图模式*/    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.basicmap:                aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式                break;            case R.id.rsmap:                aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式                break;        }    }    /**     * 定位成功后回调函数     */    @Override    public void onLocationChanged(AMapLocation amapLocation) {        if (mListener != null && amapLocation != null) {            if (amapLocation != null                    && amapLocation.getErrorCode() == 0) {                mListener.onLocationChanged(amapLocation);// 显示系统小蓝点                /*设置显示的精度级别*/                aMap.moveCamera(CameraUpdateFactory.zoomTo(17));            } else {                String errText = "定位失败," + amapLocation.getErrorCode()+ ": " + amapLocation.getErrorInfo();                Log.e("AmapErr",errText);            }        }    } /*激活定位*/    @Override    public void activate(OnLocationChangedListener listener) {        mListener = listener;        if (mlocationClient == null) {            mlocationClient = new AMapLocationClient(this);            mLocationOption = new AMapLocationClientOption();            //设置定位监听            mlocationClient.setLocationListener(this);            //设置为高精度定位模式            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);            //设置定位参数            mlocationClient.setLocationOption(mLocationOption);            // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,            // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求            // 在定位结束后,在合适的生命周期调用onDestroy()方法            // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除            mlocationClient.startLocation();        }    }    /*停止定位*/    @Override    public void deactivate() {        mListener = null;        if (mlocationClient != null) {            mlocationClient.stopLocation();            mlocationClient.onDestroy();        }        mlocationClient = null;    }}


阅读全文
0 0
原创粉丝点击