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. INSTALL_FAILED_MISSING_SHARED_LIBRARY安卓谷歌地圖
- 4. 安卓谷歌地圖nullpointerexception
- 5. 安卓谷歌地圖隨谷歌地圖更新 - 對吧?
- 6. 谷歌地圖安卓定製圖片
- 7. 安卓谷歌地圖視圖
- 8. 安卓:色調屏幕在圖形頁面(谷歌地圖)
- 9. 安卓谷歌地圖教程
- 10. 谷歌地圖安卓方向
- 11. 安卓+谷歌地圖API v2
- 12. 安卓谷歌地圖緯度/經度
- 13. 創建路線安卓谷歌地圖
- 14. 安卓谷歌地圖在片段
- 15. 安卓抽屜和谷歌地圖
- 16. 安卓谷歌地圖問題
- 17. 安卓谷歌地圖步行方向
- 18. 安卓谷歌地圖路由
- 19. 安卓谷歌室內地圖
- 20. 安卓谷歌地圖按鈕彈出
- 21. 谷歌地圖網格只有安卓
- 22. 安卓谷歌地圖v2不渲染
- 23. NullPointerException - 安卓谷歌地圖街景
- 24. 安卓谷歌地圖版本2 monodroid
- 25. 安卓谷歌地圖填充圈
- 26. 安卓谷歌地圖問題
- 27. 在多邊形安卓上顯示文本谷歌地圖v2
- 28. 安卓谷歌地圖v2多邊形性能問題?
- 29. 谷歌學院安卓谷歌地圖自動完成示例
- 30. 3D六角形地圖