0
A
回答
1
您可以实现与您在Google Maps Android API中为Google Maps JavaScript API提到的功能类似的功能。为此,您需要使用Google Maps Android API Utility Library中的SphericalUtil,并且还可以将标签添加为GroundOverlay对象。请看看下面的例子,说明如何实现这个功能
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng pos = new LatLng(33.748589, -84.390392);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, 7));
int radius = 40 * 1000; //radius in meters
drawHorizontalHexagonGrid(pos, radius,6);
mMap.getUiSettings().setZoomControlsEnabled(true);
}
private void drawHorizontalHexagonGrid(LatLng startPosition, int radius, int count){
LatLng curPos = startPosition;
double width = radius * 2 * Math.sqrt(3)/2 ;
for(int i = 0; i < count; i++) {
drawHorizontalHexagon(curPos, radius, "" + (i+1));
curPos = SphericalUtil.computeOffset(curPos, width,90);
}
}
private void drawHorizontalHexagon(LatLng position, int radius, String label){
List<LatLng> coordinates = new ArrayList<>();
for(int angle = 0; angle < 360; angle += 60) {
coordinates.add(SphericalUtil.computeOffset(position, radius, angle));
}
PolygonOptions opts = new PolygonOptions().addAll(coordinates)
.fillColor(Color.argb(35,255, 0,0))
.strokeColor(Color.RED).strokeWidth(3);
mMap.addPolygon(opts);
this.showText(position, label);
}
private void showText(LatLng pos, String label) {
mMap.addGroundOverlay(new GroundOverlayOptions().position(pos, 10000)
.image(
BitmapDescriptorFactory.fromBitmap(
getBitmapFromView(label)
)
)
.zIndex(1000)
.transparency(0)
.visible(true)
);
}
private Bitmap getBitmapFromView(String label) {
Bitmap myRefBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.transparent);
Bitmap myWrittenBitmap = Bitmap.createBitmap(myRefBitmap.getWidth(),
myRefBitmap.getHeight(), Bitmap.Config.ARGB_4444);
float scale = getResources().getDisplayMetrics().density;
Canvas canvas = new Canvas(myWrittenBitmap);
Paint txtPaint = new Paint();
txtPaint.setColor(Color.BLUE);
txtPaint.setTextSize(48*scale);
txtPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
txtPaint.setTypeface(Typeface.DEFAULT_BOLD);
//draw ref bitmap then text on our canvas
canvas.drawBitmap(myRefBitmap, 0, 0, null);
canvas.drawText(label, 5, myRefBitmap.getHeight(), txtPaint);
return myWrittenBitmap;
}
}
你可以看到下面的截图结果
完整的示例项目可在Github上:
https://github.com/xomena-so/so45856063
请用您的API代替。
我希望这有助于!
相关问题
- 1. 谷歌地图安卓点多边形
- 2. 安卓谷歌地图
- 3. 安卓谷歌地图视图
- 4. 安卓:色调屏幕在图形页面(谷歌地图)
- 5. 谷歌地图网格只有安卓
- 6. 安卓抽屉和谷歌地图
- 7. 安卓谷歌地图问题
- 8. NullPointerException - 安卓谷歌地图街景
- 9. 安卓谷歌地图多边形添加圆孔
- 10. 科尔多瓦谷歌地图安卓空白地图
- 11. Android谷歌地图地形
- 12. 显示在谷歌地图搜索附近的地方安卓
- 13. 角谷歌地图显示
- 14. 角谷歌地图标记
- 15. 安卓在谷歌地图上的图钉
- 16. 添加图片到谷歌地图标记标题中安卓
- 17. 安卓:你好,视图>谷歌地图查看Tuorial
- 18. 安卓谷歌地图图标标记只是颜色白色
- 19. Android谷歌地图圆形
- 20. 设置谷歌地图边界与角谷歌地图
- 21. 安卓 - 谷歌日历
- 22. 安卓+谷歌API v2
- 23. 安卓在谷歌地图上绘制多边形后请求HTTP请求
- 24. 谷歌地图不安装?
- 25. 在java中绘制六角形地图
- 26. 安卓谷歌地图API在发布不起作用
- 27. 安卓谷歌地图路径在变焦过程中消失
- 28. 安卓谷歌地图移动相机不起作用
- 29. 安卓谷歌地图API V2应用在片段内
- 30. 安卓谷歌地图在Nexus S上崩溃