高德地图实现根据指定经纬度显示地图

时间:2024-03-06 11:54:28

1.导入的jar包

 

2.权限:

 

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
View Code

3.加入key值

 

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data android:name="com.amap.api.v2.apikey" android:value="e9c74e3b5975424f5a123a405878bd23"></meta-data>
View Code

4.xml文件

 

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.myapplicationmap.MainActivity">

   <com.amap.api.maps2d.MapView
       android:id="@+id/map"
       android:layout_height="match_parent"
       android:layout_width="match_parent">
       </com.amap.api.maps2d.MapView>
</RelativeLayout>
View Code

5.MainActivity

package com.example.administrator.myapplicationmap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.GroundOverlay;
import com.amap.api.maps2d.model.GroundOverlayOptions;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.LatLngBounds;
import com.amap.api.maps2d.model.MarkerOptions;


public class MainActivity extends AppCompatActivity {
      MapView mMapView = null;
      LatLng ll;
    private GroundOverlay groundoverlay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);

        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),实现地图生命周期管理
        mMapView.onCreate(savedInstanceState);
        AMap aMap = mMapView.getMap();

        //AMap aMap = mMapView.getMap();
        LatLng latLng = new LatLng(30.272116, 120.096134);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 19));

        //设置远小近大效果,设置刷新一次图片资源的周期。
        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(latLng);
        markerOption.title("西安市").snippet("西安市:34.341568, 108.940174");
        markerOption.draggable(true);
        markerOption.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
        aMap.addMarker(markerOption);

       // aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.936713, 116.386475), 19));
//设置图片的显示区域。
//        LatLngBounds bounds = new LatLngBounds.Builder()
//                .include(new LatLng(39.935029, 116.384377))
//                .include(new LatLng(39.939577, 116.388331)).build();
//
//        groundoverlay = aMap.addGroundOverlay(new GroundOverlayOptions()
//                .anchor(0.5f, 0.5f).transparency(0.1f)
//                .image(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
//                .positionFromBounds(bounds));
//


    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理
        mMapView.onSaveInstanceState(outState);
    }

}
View Code