如何使用 Google MAps API v2 围绕引脚绘制圆圈
Posted
技术标签:
【中文标题】如何使用 Google MAps API v2 围绕引脚绘制圆圈【英文标题】:How to draw circle around a pin using Google MAps API v2 【发布时间】:2012-12-30 18:24:41 【问题描述】:我正在为我的 android 应用程序使用新的 API(Google Map API V2),我已经创建了地图并为其添加了标记,现在我的任务是手动围绕任何标记创建一个圆圈,我也想要为用户提供一个功能,他可以相应地增加该圆的半径,为此我给出了一个条,当用户增加该条时,圆的半径将增加,反之亦然。
如果有人知道如何使用 Google Map API V2 做到这一点,请帮助,
谢谢
【问题讨论】:
参考***.com/questions/13991301/… 【参考方案1】:我也一直在研究这个问题,我找到了以下解决方案。它仍然不完美,因为我必须制作一个非常大的 Canvas 以防止圆的边缘变得模糊。
private void addCircleToMap()
// circle settings
int radiusM = // your radius in meters
double latitude = // your center latitude
double longitude = // your center longitude
LatLng latLng = new LatLng(latitude,longitude);
// draw circle
int d = 500; // diameter
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint();
p.setColor(getResources().getColor(R.color.green));
c.drawCircle(d/2, d/2, d/2, p);
// generate BitmapDescriptor from circle Bitmap
BitmapDescriptor bmD = BitmapDescriptorFactory.fromBitmap(bm);
// mapView is the GoogleMap
mapView.addGroundOverlay(new GroundOverlayOptions().
image(bmD).
position(latLng,radiusM*2,radiusM*2).
transparency(0.4f));
-- 编辑-- 谷歌更新了 API。您现在可以轻松地将圆圈添加到地图中: https://developers.google.com/maps/documentation/android/shapes?hl=nl#circles
【讨论】:
感谢您提供的宝贵代码。它对我帮助很大。但是如何设置该圆圈的中心?我需要中心作为标记本身.. 您编辑的答案对我帮助很大。谢谢。 @spes:groundoverlay 在放大/缩小地图时会改变其大小。有什么办法可以保持不变?【参考方案2】:Google 制作的地图 v2 很简单。下面的 sn-p 演示了绘制标记和圆圈以及一起更新它们的位置。
private Circle mCircle;
private Marker mMarker;
private GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener()
@Override
public void onMyLocationChange(Location location)
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
if(mCircle == null || mMarker == null)
drawMarkerWithCircle(latLng);
else
updateMarkerWithCircle(latLng);
);
private void updateMarkerWithCircle(LatLng position)
mCircle.setCenter(position);
mMarker.setPosition(position);
private void drawMarkerWithCircle(LatLng position)
double radiusInMeters = 100.0;
int strokeColor = 0xffff0000; //red outline
int shadeColor = 0x44ff0000; //opaque red fill
CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(8);
mCircle = mGoogleMap.addCircle(circleOptions);
MarkerOptions markerOptions = new MarkerOptions().position(position);
mMarker = mGoogleMap.addMarker(markerOptions);
【讨论】:
【参考方案3】:这样更好:
double radiusInMeters = 100.0;
//red outline
int strokeColor = 0xffff0000;
//opaque red fill
int shadeColor = 0x44ff0000;
CircleOptions circleOptions = new CircleOptions().center(position).radius(radiusInMeters).fillColor(shadeColor).strokeColor(strokeColor).strokeWidth(2);
mCircle = map.addCircle(circleOptions);
MarkerOptions markerOptions = new MarkerOptions().position(position);
mMarker = map.addMarker(markerOptions);
【讨论】:
【参考方案4】:也许对你有帮助:
GoogleMap map;
// ... get a map.
// Add a circle in Sydney
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(-33.87365, 151.20689))
.radius(10000)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
从这里:
HERE
【讨论】:
【参考方案5】:使用此方法,您可以选择任何标记,它将为特定标记创建圆形对象。 您可以通过将标记对象和半径值传递给 createCircle() 方法来动态更改圆的半径。
private GoogleMap mMap;
/*Create circle objects*/
Circle currentCircle;
/**
* create circle when user want to set region
* @param currentMarker this is user selected marker
* @param radius pass radius value to circle object
*/
private void createCircle(Marker currentMarker ,Double radius)
//check circle is exist or not
//if exist remove
if(currentCircle!=null)
currentCircle.remove();
currentCircle=mMap.addCircle(new CircleOptions().center(currentMarker.getPosition()).radius(radius)
.strokeColor(Color.parseColor("#FF007A93"))
.fillColor(Color.parseColor("#40007A93"))
.strokeWidth(2));
float zoomLevel = getZoomLevel(radius);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentMarker.getPosition(), zoomLevel));
【讨论】:
以上是关于如何使用 Google MAps API v2 围绕引脚绘制圆圈的主要内容,如果未能解决你的问题,请参考以下文章