作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
最佳答案
您可以在 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 key 替换为您的 key 。
希望这会有所帮助!
关于java - Android 谷歌地图六角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45856063/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!