一、准备工作
1、获取LatLng对象
2、可以通过BitmapDescriptorFactory获得一个BitmapDescriptor对象。
3、定义了一个marker的选项
二、核心代码:
Intent intent = getIntent(); //获取维度 double latitude = intent.getDoubleExtra(getString(R.string.intent_location_latitude),0); //获取经度 double longitude = intent.getDoubleExtra(getString(R.string.intent_location_longitude),0); //定位得到的地址 String address = intent.getStringExtra(getString(R.string.intent_location_address)); if (latitude != 0 && longitude != 0 && !TextUtils.isEmpty(address)){ //更改定位图标 LatLng latLng = new LatLng(latitude, longitude); //在高德地图API 里,如果需要将一张图片绘制为Marker,需要用这个类把图片包装成对象, // 可以通过BitmapDescriptorFactory 获得一个BitmapDescriptor 对象。 ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); giflist.add(BitmapDescriptorFactory.fromResource(R.mipmap.icon_map_location)); //定义了一个marker 的选项 MarkerOptions markerOption1 = new MarkerOptions() .anchor(0.5f, 0.5f)//定义marker 图标的锚点。 .position(latLng).title("")//设置 Marker 的标题 .snippet(address)//设置 Marker 上的 snippet即文字片段 .icons(giflist)//设置MarkerOptions 对象的自定义图标 .draggable(true)//设置标记是否可拖动。 .period(50);//设置多少帧刷新一次图片资源,Marker动画的间隔时间,值越小动画越快 ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>(); markerOptionlst.add(markerOption1); List<Marker> markerlst = fragment_a_map.addMarkers(markerOptionlst, true); this.resultString = new StringBuffer(address); }
其中注意:经纬度,在这里是另一个activity通过Intent对象传递过来的。
三、效果图: