我正在使用apiV2在Google地图中显示一个带精度光盘的圆圈和一个以圆圈为中心的标记。
onLocationChanged我正在删除现有的圈子/标记。 onMapReady我正在绘制新的圆圈/标记。
我试图在#34;之后画出Marker"代码中的圆圈。我试着在标记上调用.showInfoWindow(),但即便如此,它仍然显示在圆圈后面。
这是我的代码(显然没有完成):
public void onLocationChanged(Location location) {
newLatLng = new LatLng(location.getLatitude(), location.getLongitude());
//Circle management
if (myLocationCircle != null) {
myLocationCircle.remove();
}
circleOptions = new CircleOptions()
.center(newLatLng)
.radius(location.getAccuracy())
.strokeWidth(2)
.strokeColor(Color.BLUE)
.fillColor(Color.parseColor("#500084d3")); //Format are: #RRGGBB #AARRGGBB, where #AA is the transparency
//Marker management
if (myLocationMarker != null) {
myLocationMarker.remove();
}
markerOptions = new MarkerOptions()
.position(newLatLng)
.title("Hey, that's my position!");
onMapReady(mGoogleMap);
}
@Override
public void onMapReady(GoogleMap myGoogleMap) {
if (myGoogleMap == null) {
Toast.makeText(this.getApplicationContext(),"Sorry! Unable to initialize the map", Toast.LENGTH_LONG).show();
}
else {
mGoogleMap = myGoogleMap;
myGoogleMap.setMyLocationEnabled(true);
myGoogleMap.getUiSettings().setZoomControlsEnabled(true);
myGoogleMap.getUiSettings().setAllGesturesEnabled(true);
myGoogleMap.getUiSettings().setCompassEnabled(true);
myGoogleMap.getUiSettings().setMapToolbarEnabled(true);
myGoogleMap.setMyLocationEnabled(true);
myCurrentCameraPosition = myGoogleMap.getCameraPosition();
if ( newLatLng != null ) {
CameraPosition myNewCameraPosition = new CameraPosition(newLatLng, 15, myCurrentCameraPosition.tilt, myCurrentCameraPosition.bearing);
myGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(myNewCameraPosition));
if (circleOptions!=null){
//Draw blue circle around your position (setup of options done in onLocationChange())
myLocationCircle = myGoogleMap.addCircle(circleOptions);
}
if (markerOptions != null) {
//Draw a marker at your position (setup of options done in onLocationChange())
myLocationMarker = mGoogleMap.addMarker(markerOptions);
myLocationMarker.showInfoWindow();
}
}
}
}
答案 0 :(得分:1)
请查看此代码,该代码演示绘图标记和圆圈以及更新其位置。
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);
}