Java Code Examples for org.osmdroid.views.overlay.Marker#setAnchor()

The following examples show how to use org.osmdroid.views.overlay.Marker#setAnchor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 7 votes vote down vote up
public static void addMarker(Context context, MapView map, Element element) {
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(element.lat, element.lon));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(marker);
    map.invalidate();
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));

    marker.setTitle(String.valueOf(""));
    for (Map.Entry<String, String> tag : element.tags.entrySet()) {
        if (tag.getKey().equals("name")) {
            marker.setTitle(String.valueOf(tag.getValue()));
            break;
        }
    }
    if (marker.getTitle().equals(""))
        marker.setTitle(String.valueOf(element.id));

}
 
Example 2
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
private void createNativeMarker(final MapMarker aiMarker,
    AsyncCallbackPair<Marker> callback) {
  final Marker osmMarker = new Marker(view);
  featureOverlays.put(aiMarker, osmMarker);
  osmMarker.setDraggable(aiMarker.Draggable());
  osmMarker.setTitle(aiMarker.Title());
  osmMarker.setSnippet(aiMarker.Description());
  osmMarker.setPosition(new GeoPoint(aiMarker.Latitude(), aiMarker.Longitude()));
  osmMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
  getMarkerDrawable(aiMarker, new AsyncCallbackFacade<Drawable, Marker>(callback) {
    @Override
    public void onFailure(String message) {
      callback.onFailure(message);
    }

    @Override
    public void onSuccess(Drawable result) {
      osmMarker.setIcon(result);
      callback.onSuccess(osmMarker);
    }
  });
}
 
Example 3
Source File: MapsActivity.java    From Finder with GNU General Public License v3.0 5 votes vote down vote up
private void trackRedraw() {
    Cursor query =  db.query("tracking_table", new String[] {"_id", "lat", "lon", "speed", "date"}, "track_id = ?", new String[] {String.valueOf(track_id)}, null, null, "_id ASC");

    Cursor last_point =  db.rawQuery("SELECT lat, lon, _id FROM tracking_table WHERE _id = (SELECT MAX(_id) FROM tracking_table WHERE track_id = ?)", new String[] {String.valueOf(track_id)});
    last_point.moveToFirst();
    Double lat = last_point.getDouble(0);
    Double lon = last_point.getDouble(1);
    last_point.close();

    //zoom to latest point, because BoundBox don't work in OsmDroid library (this is known lib bug)
    mapController.setCenter(new GeoPoint(lat, lon));
    mapController.setZoom(15d);
    List<GeoPoint> track = new ArrayList<>();

    while (query.moveToNext()) {
        GeoPoint gp = new GeoPoint(query.getDouble(1), query.getDouble(2));
        track.add(gp);
        Marker marker = new Marker(map);
        marker.setPosition(gp);
        marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
        map.getOverlays().add(marker);
        marker.setTitle(getString(R.string.date_time, query.getString(4), query.getFloat(3)));
    }
    query.close();
    line.setPoints(track);
    GeoPoint center = new GeoPoint(lat, lon);
    mapController.setCenter(center);
}
 
Example 4
Source File: NativeOpenStreetMapController.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
@Override
public void updateFeaturePosition(MapMarker aiMarker) {
  Marker marker = (Marker)featureOverlays.get(aiMarker);
  if (marker != null) {
    marker.setAnchor(ANCHOR_HORIZONTAL[aiMarker.AnchorHorizontal()],
        ANCHOR_VERTICAL[aiMarker.AnchorVertical()]);
    marker.setPosition(new GeoPoint(aiMarker.Latitude(), aiMarker.Longitude()));
    view.invalidate();
  }
}
 
Example 5
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Add new {@link Marker} to {@link MapView}
 *
 * @param context  The context to use. Usually your {@link android.app.Application} or {@link android.app.Activity} object.
 * @param map      {@link MapView} to add marker on
 * @param position Location of the marker
 */
public static Marker addMarker(Context context, MapView map, GeoPoint position) {
    Marker marker = new Marker(map);
    marker.setPosition(position);
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setTitle("Latitude: " + position.getLatitude() + "\n" + "Longitude: " + position.getLongitude());
    marker.setPanToView(true);
    marker.setDraggable(true);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 6
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static void addMarker(Context context, MapView map, double latitude, double longitude, String markerTitle) {
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(marker);
    map.invalidate();
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setTitle(markerTitle);
}
 
Example 7
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static Marker addMarker(Context context, MapView map, double latitude, double longitude) {
    if (map == null || context == null) return null;
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_location));
    marker.setInfoWindow(null);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 8
Source File: MapUtils.java    From AndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
public static Marker myLocation(Context context, MapView map, double latitude, double longitude) {
    if (map == null || context == null) return null;
    Marker marker = new Marker(map);
    marker.setPosition(new GeoPoint(latitude, longitude));
    marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    marker.setIcon(context.getResources().getDrawable(R.drawable.ic_gps_location));
    marker.setInfoWindow(null);
    map.getOverlays().add(marker);
    map.invalidate();
    return marker;
}
 
Example 9
Source File: MarkerDrag.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
public void addOverlays(){
    super.addOverlays();
    //0. Using the Marker overlay
    Marker startMarker = new Marker(mMapView);
    startMarker.setPosition(new GeoPoint(0.0,0.0));
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    startMarker.setTitle("Start point");
    startMarker.setDraggable(true);
    startMarker.setOnMarkerDragListener(new OnMarkerDragListenerDrawer());
    mMapView.getOverlays().add(startMarker);

}
 
Example 10
Source File: WeathForceActivity.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_starter_mapview);

    Intent intent = getIntent();
    //if (intent)
    final double lat1 = 25.633;
    final double long1 = 71.094;

    //super important. Many tile servers, including open street maps, will BAN applications by user
    //agent. Do not use the sample application's user agent for your app! Use your own setting, such
    //as the app id.
    Configuration.getInstance().setUserAgentValue(getPackageName());

    mMapView = findViewById(R.id.mapview);
    mMapView.setTileSource(TileSourceFactory.MAPNIK);


    mCompassOverlay = new CompassOverlay(this, new InternalCompassOrientationProvider(this),
            mMapView);
    mCompassOverlay.enableCompass();
    mMapView.getOverlays().add(this.mCompassOverlay);

    addOverlays();

    GeoPoint startPoint = new GeoPoint(lat1, long1);
    IMapController mapController = mMapView.getController();
    mapController.setZoom(9);
    mapController.setCenter(startPoint);
    Marker startMarker = new Marker(mMapView);
    startMarker.setPosition(startPoint);
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    mMapView.getOverlays().add(startMarker);


    mMapView.invalidate();

}
 
Example 11
Source File: MapsActivity.java    From Finder with GNU General Public License v3.0 4 votes vote down vote up
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Context ctx = getApplicationContext();
    Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
    Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);

    setContentView(R.layout.activity_open_map);
    SharedPreferences sPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    map = findViewById(R.id.map2);
    Configuration.getInstance().setTileFileSystemCacheMaxBytes(1024*1024*Long.parseLong(sPref.getString("cache_size", "5")));
    Configuration.getInstance().setTileFileSystemCacheTrimBytes(512*1024*Long.parseLong(sPref.getString("cache_size", "5")));  // trim storage to ~50% from max after oversize limit


    if (sPref.getBoolean("satellite", false)) {
        final MapBoxTileSource tileSource = new MapBoxTileSource();
        tileSource.retrieveAccessToken(this);
        tileSource.retrieveMapBoxMapId(this);
        map.setTileSource(tileSource);
    } else {
        map.setTileSource(TileSourceFactory.MAPNIK);
    }


    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    mapController = map.getController();

    CopyrightOverlay copyOverlay = new CopyrightOverlay(this);
    copyOverlay.setAlignRight(true);
    map.getOverlays().add(copyOverlay);

    ScaleBarOverlay scaleBar= new ScaleBarOverlay(map);
    scaleBar.setUnitsOfMeasure(ScaleBarOverlay.UnitsOfMeasure.metric);
    scaleBar.setScaleBarOffset(getResources().getDisplayMetrics().widthPixels/2, 50);
    scaleBar.setCentred(true);
    map.getOverlays().add(scaleBar);

    GpsMyLocationProvider gpsProvider = new GpsMyLocationProvider(this.getBaseContext());
    MyLocationNewOverlay myLoc = new MyLocationNewOverlay(gpsProvider, map);
    myLoc.setDrawAccuracyEnabled(true);
    map.getOverlays().add(myLoc);

    CompassOverlay compasOver = new CompassOverlay(this.getBaseContext(), map);
    compasOver.enableCompass();
    map.getOverlays().add(compasOver);

    dBase baseConnect = new dBase(this);
    db = baseConnect.getWritableDatabase();

    Intent intent = this.getIntent();
    mapController.setZoom(intent.getDoubleExtra("zoom", 15.0d));
    String act = intent.getAction();
    if (act.equals("track")) {
        track_id = intent.getIntExtra("track_id", 0);
        line = new Polyline(map);
        line.setColor(Color.BLUE);
        line.setInfoWindow(null);  //delete popup label from track
        line.setGeodesic(true);  //draw "true" lines instead simple lines (great circles)
        trackRedraw();
        map.getOverlays().add(line);
    } else {
        String accuracy = intent.getStringExtra("accuracy");
        GeoPoint startPoint = new GeoPoint(intent.getDoubleExtra("lat", 0d), intent.getDoubleExtra("lon", 0d));
        mapController.setCenter(startPoint);
        Marker startMarker = new Marker(map);
        startMarker.setPosition(startPoint);
        startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
        map.getOverlays().add(startMarker);
        startMarker.setTitle(accuracy);
    }

    //receiver to map updating
    LocalBroadcastManager.getInstance(this).registerReceiver(updMap, new IntentFilter("update_map"));
}
 
Example 12
Source File: MilStdPointPlottingOverlay.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onLongPress(final MotionEvent e, final MapView mapView) {
    if (def != null) {
        GeoPoint pt = (GeoPoint) mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY(), null);

        //just in case the point is off the map, let's fix the coordinates
        if (pt.getLongitude() < -180)
            pt.setLongitude(pt.getLongitude() + 360);
        if (pt.getLongitude() > 180)
            pt.setLongitude(pt.getLongitude() - 360);
        //latitude is a bit harder. see https://en.wikipedia.org/wiki/Mercator_projection
        if (pt.getLatitude() > mapView.getTileSystem().getMaxLatitude())
            pt.setLatitude(mapView.getTileSystem().getMaxLatitude());
        if (pt.getLatitude() < mapView.getTileSystem().getMinLatitude())
            pt.setLatitude(mapView.getTileSystem().getMinLatitude());

        String code = def.getSymbolCode().replace("*", "-");
        //TODO if (!def.isMultiPoint())
        {
            int size = 128;

            SparseArray<String> attr = new SparseArray<>();
            attr.put(MilStdAttributes.PixelSize, size + "");

            ImageInfo ii = MilStdIconRenderer.getInstance().RenderIcon(code, def.getModifiers(), attr);
            Marker m = new Marker(mapView);
            m.setPosition(pt);
            m.setTitle(code);
            m.setSnippet(def.getDescription() + "\n" + def.getHierarchy());
            m.setSubDescription(def.getPath() + "\n" + m.getPosition().getLatitude() + "," + m.getPosition().getLongitude());

            if (ii != null && ii.getImage() != null) {
                BitmapDrawable d = new BitmapDrawable(ii.getImage());
                m.setImage(d);
                m.setIcon(d);

                int centerX = ii.getCenterPoint().x;    //pixel center position
                //calculate what percentage of the center this value is
                float realCenterX = (float) centerX / (float) ii.getImage().getWidth();

                int centerY = ii.getCenterPoint().y;
                float realCenterY = (float) centerY / (float) ii.getImage().getHeight();
                m.setAnchor(realCenterX, realCenterY);


                mapView.getOverlayManager().add(m);
                mapView.invalidate();
            }
        }

        return true;
    }
    return false;
}
 
Example 13
Source File: Plotter.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.radio_milstd2525b:
        case R.id.radio_milstd2525c:
            if (((RadioButton) v).isChecked()) {
                RendererSettings.getInstance().setSymbologyStandard(RendererSettings.Symbology_2525C);
            } else
                RendererSettings.getInstance().setSymbologyStandard(RendererSettings.Symbology_2525B);
            break;
        case R.id.cancelAddIcon:
            picker.dismiss();
            break;
        case R.id.addIcon:
            //from the menu, user entered code
            String code = symbolCode.getText().toString();
            int size = 128;
            try {
                size = Integer.parseInt(symbolSize.getText().toString());
            } catch (Exception ex) {
            }
            String baseCode = SymbolUtilities.getBasicSymbolID(code);
            SymbolDef def = SymbolDefTable.getInstance().getSymbolDef(baseCode, RendererSettings.getInstance().getSymbologyStandard());

            SparseArray<String> attr = new SparseArray<>();
            attr.put(MilStdAttributes.PixelSize, size + "");

            ImageInfo ii = mir.RenderIcon(code, new SparseArray<String>(), attr);
            Marker m = new Marker(mMapView);
            m.setPosition((GeoPoint) mMapView.getMapCenter());
            m.setTitle(code);
            if (def != null) {
                m.setSubDescription(def.getFullPath());
                m.setSnippet(def.getDescription() + "\n" + def.getHierarchy());
            }
            Drawable d = new BitmapDrawable(ii.getImage());
            m.setImage(d);
            m.setIcon(d);
            int centerX = ii.getCenterPoint().x;    //pixel center position
            //calculate what percentage of the center this value is
            float realCenterX = (float) centerX / (float) ii.getImage().getWidth();

            int centerY = ii.getCenterPoint().y;
            float realCenterY = (float) centerY / (float) ii.getImage().getHeight();
            m.setAnchor(realCenterX, realCenterY);
            mMapView.getOverlayManager().add(m);
            mMapView.invalidate();
            picker.dismiss();

            //TODO store the symbol code and size as an android preference
            SharedPreferences.Editor edit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
            edit.putString("MILSTDCODE", code);
            RendererSettings.getInstance().setDefaultPixelSize(size);
            edit.putInt("MILSTDSIZE", size);
            edit.commit();

            break;
        case R.id.enablePanning:
            enablePanning();

            break;
        case R.id.enablePainting:
            enablePainting();
            break;
    }
}
 
Example 14
Source File: SampleMapCenterOffset.java    From osmdroid with Apache License 2.0 4 votes vote down vote up
@Override
public void addOverlays() {
	super.addOverlays();

	final Drawable drawable = getResources().getDrawable(R.drawable.marker_default);

	mList.add(new GeoPoint(38.8977, -77.0365));  // white house
	mList.add(new GeoPoint(38.8719, -77.0563));  // pentagon
	mList.add(new GeoPoint(38.8895, -77.0353));  // washington monument

	for (final GeoPoint geoPoint : mList) {
		final Marker startMarker = new Marker(mMapView);
		startMarker.setPosition(geoPoint);
		startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
		startMarker.setIcon(drawable);
		mMapView.getOverlays().add(startMarker);
	}

	mPaint.setColor(Color.RED);
	mPaint.setStrokeWidth(5);

	mMapView.getOverlays().add(new Overlay() {

		@Override
		public void draw(Canvas pCanvas, Projection pProjection) {
			mMapView.getProjection().save(pCanvas, false, true);
			final float centerX = pCanvas.getWidth() / 2f;
			final float centerY = pCanvas.getHeight() / 2f;
			pCanvas.drawLine(centerX, centerY, centerX + mOffsetX, centerY + mOffsetY, mPaint);
			mMapView.getProjection().restore(pCanvas, true);
		}
	});

	mMapView.setMapCenterOffset(mOffsetX, mOffsetY);
	mMapView.post(new Runnable() {
		@Override
		public void run() {
			show();
		}
	});
}