记录高德地图API接入Android原生项目(三)

我们在App接入地图,有时候想要在地图上显示某个坐标地址。假如给出一个定位坐标(假设为高德地图的坐标,其他坐标可能有所不同,需要转换),需要在地图上显示该坐标,比如点击某个店铺,显示该店铺的位置。下面简单介绍代码:

1.布局文件:地图控件是MapView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        >
    </com.amap.api.maps.MapView>
</LinearLayout>

2.Activity代码:

package com.my.gaodemapdemo;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;

import static com.amap.api.maps.AMap.MAP_TYPE_NORMAL;

public class TestActivity3 extends AppCompatActivity {

    MapView mapView;
    AMap aMap;
    double latitude = 23.119828;//需要显示的定位坐标
    double longitude = 113.315197;//需要显示的定位坐标
    String postr = "珠江新城测试点";//点击显示的测试点名称
    String posAddress="广州越秀区珠江新城";//点击显示测试点的地址
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test3_layout);
        mapView=this.findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        aMap = mapView.getMap();
        initData();
    }


    private void initData() {
        aMap = mapView.getMap();
        LatLng latLng = new LatLng(latitude,longitude);//默认显示位置
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,18));
        // 如果要设置定位的默认状态,可以在此处进行设置
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.strokeColor(Color.TRANSPARENT);// 设置圆形的边框颜色
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色
        myLocationStyle.strokeWidth(1.0f);// 设置圆形的边框粗细
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setMyLocationStyle(myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE));
        aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(false);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
        aMap.setMapType(MAP_TYPE_NORMAL);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));//地图默认缩放比例
        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(new LatLng(latitude,longitude));
        markerOption.draggable(false);//设置Marker可拖动
        markerOption.title(postr);//设置标题
        markerOption.snippet(posAddress);//设置内容
        aMap.addMarker(markerOption);
    }

}

3.最后看效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值