私はAndroidでマップのこの機能を複製しようとしています:
マップ上に、ユーザーが選択した範囲を表す円があることがわかります。
私のアプリケーションでは、円の周囲にドラッガーを配置し、ドラッグして半径を再定義することもできます。
誰かがカスタムの描画可能なオーバーレイと2Dグラフィックスをマップ上に描画する方法を教えてくれれば、私は自分で他のことを行うことができます。
ありがとう!
完全なアプリケーションは、このリンクからアクセスできます
私はAndroidでマップのこの機能を複製しようとしています:
マップ上に、ユーザーが選択した範囲を表す円があることがわかります。
私のアプリケーションでは、円の周囲にドラッガーを配置し、ドラッグして半径を再定義することもできます。
誰かがカスタムの描画可能なオーバーレイと2Dグラフィックスをマップ上に描画する方法を教えてくれれば、私は自分で他のことを行うことができます。
ありがとう!
完全なアプリケーションは、このリンクからアクセスできます
さて、私は自分で物事をやろうとしました、そして上記の効果を得るためにこのコードを入れました:
public class MarkerOverlay extends Overlay {
Geocoder geoCoder = null;
public MarkerOverlay() {
super();
}
@Override
public boolean onTap(GeoPoint geoPoint, MapView mapView){
selectedLatitude = geoPoint.getLatitudeE6();
selectedLongitude = geoPoint.getLongitudeE6();
return super.onTap(geoPoint,mapView);
}
@Override
public void draw(Canvas canvas, MapView mapV, boolean shadow){
if(shadow){
Projection projection = mapV.getProjection();
Point pt = new Point();
projection.toPixels(globalGeoPoint,pt);
GeoPoint newGeos = new GeoPoint(selectedLat+(100),selectedLong); // adjust your radius accordingly
Point pt2 = new Point();
projection.toPixels(newGeos,pt2);
float circleRadius = Math.abs(pt2.y-pt.y);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
circlePaint.setColor(0x99000000);
circlePaint.setStyle(Style.STROKE);
canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
Bitmap markerBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.pin);
canvas.drawBitmap(markerBitmap,pt.x,pt.y-markerBitmap.getHeight(),null);
super.draw(canvas,mapV,shadow);
}
}
}
これにより、次のような効果が得られます。
使用される計算は、希望どおりでない場合があります。
それはデモンストレーションの目的のためだけです。
実際の距離/距離の計算には、方位も使用する必要があり、特定の式があります。
ご不明な点がございましたら、お気軽にお問い合わせください。
クラスItemizedOverlayを拡張して、 draw()メソッドをオーバーライドします。Canvas
オーバーレイが描画される場所はそのメソッドに渡され、drawCircleまたは範囲ドラッガーを表示するために必要なものを呼び出すことができます。
円で画鋲を描くためのサンプルコード:
public class MapDemoActivity extends MapActivity {
MapView mapView;
MapController mc;
GeoPoint p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);
View zoomView = mapView.getZoomControls();
zoomLayout.addView(zoomView,
new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mapView.displayZoomControls(true);
mc = mapView.getController();
String coordinates[] = {"28.38", "77.12"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(8);
//---Add a location marker---
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapView.invalidate();
}
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//--------------draw circle----------------------
Point pt=mapView.getProjection().toPixels(p,screenPts);
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(0x30000000);
circlePaint.setStyle(Style.FILL_AND_STROKE);
canvas.drawCircle(screenPts.x, screenPts.y, 50, circlePaint);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pin);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-bmp.getHeight(), null);
super.draw(canvas,mapView,shadow);
return true;
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}