使用 IndoorAtlas 显示 FloorPlan 并获取位置
Show FloorPlan and get Location with IndoorAtlas
是否有任何关于如何使用 IndoorAtlas SDK 的资源?
我对如何显示 floorPlan 和获取当前位置感到困惑。
请帮助我。
大致方法如下:
1) 初始化 IndoorAtlas 实例:
IndoorAtlas ia = IndoorAtlasFactory.createIndoorAtlas(context, listener, apiKey, apiSecret);
2) 获取FloorPlan实例:
FutureResult<FloorPlan> result = ia.fetchFloorPlan(floorPlanId);
result.setCallback(new ResultCallback<FloorPlan>() {
@Override
public void onResult(final FloorPlan result) {
mFloorPlan = result;
loadFloorPlanImage(result);
}
// handle error conditions too
}
3) 获取实际图像:
void loadFloorPlanImage(FloorPlan floorPlan) {
BitmapFactory.Options options = createBitmapOptions(floorPlan);
FutureResult<Bitmap> result = ia.fetchFloorPlanImage(floorPlan, options);
result.setCallback(new ResultCallback<Bitmap>() {
@Override
public void onResult(final Bitmap result) {
// now you have floor plan bitmap, do something with it
updateImageViewInUiThread(result);
}
// handle error conditions too
}
}
4)开始定位:
ia.startPositioning(venueId, floorId, floorPlanId);
5) 在平面图上显示位置:
public void onServiceUpdate(ServiceState state) {
// get position on original floor plan image
int i = state.getImagePoint().getI();
int j = state.getImagePoint().getJ();
// take into account how your floor plan image has been scaled
// and draw position
PointF scaledPoint = new PointF();
Util.calculateScaledPoint((int) floorPlan.dimensions[0], (int) floorPlan.dimensions[1], i, j, mImageView, scaledPoint);
drawNewPositionInUiThread(scaledPoint.x, scaledPoint.y);
}
当然可以先定位再获取图像。您也可以在本地缓存图像,但如前所述,大致就是这样。
Util.java:
public class Utils {
/**
* Calculates scaling factor for an image with original dimensions of
* {@code originalWidth x originalHeight} being displayed with {@code imageView}.
*
* The assumption with this example code is that a) layout has been already performed for
* {@code imageView} and that {@link android.widget.ImageView.ScaleType#CENTER_INSIDE} is used.
*
* @param originalWidth height of the original bitmap to be displayed using {@code imageView}
* @param originalHeight width of the original bitmap to be displayed using {@code imageView}
*/
public static float calculateScaleFactor(int originalWidth, int originalHeight,
ImageView imageView) {
if (imageView.getScaleType() != ImageView.ScaleType.CENTER_INSIDE) {
throw new IllegalArgumentException("only scale type of CENTER_INSIDE supported, was: "
+ imageView.getScaleType());
}
final int availableX = imageView.getWidth()
- (imageView.getPaddingLeft() + imageView.getPaddingRight());
final int availableY = imageView.getHeight()
- (imageView.getPaddingTop() + imageView.getPaddingBottom());
if (originalWidth > availableX || originalHeight > availableY) {
// original image would not fit without scaling
return originalWidth > availableX
? availableX / (float) originalWidth
: availableY / (float) originalHeight;
} else {
return 1f; // no scaling required
}
}
/**
* Calculates point where to draw coordinates {@code x} and {@code y} in a bitmap that's
* original dimensions were {@code originalWidth x originalHeight} and may now be scaled down
* as it's been displayed with {@code imageView}.
*
* @param originalWidth width of the original bitmap before any scaling
* @param originalHeight height of the original bitmap before any scaling
* @param x x-coordinate on original bitmap
* @param y y-coordinate on original bitmap
* @param imageView view that will be used to display bitmap
* @param point point where result value is to be stored
* @see #calculateScaleFactor(int, int, ImageView)
*/
public static void calculateScaledPoint(int originalWidth, int originalHeight,
int x, int y,
ImageView imageView,
PointF point) {
final float scale = calculateScaleFactor(originalWidth, originalHeight, imageView);
final float scaledWidth = originalWidth * scale;
final float scaledHeight = originalHeight * scale;
// when image inside view is smaller than the view itself and image is centered (assumption)
// there will be some empty space around the image (here offset)
final float offsetX = Math.max(0, (imageView.getWidth() - scaledWidth) / 2);
final float offsetY = Math.max(0, (imageView.getHeight() - scaledHeight) / 2);
point.x = offsetX + (x * scale);
point.y = offsetY + (y * scale);
}
}
是否有任何关于如何使用 IndoorAtlas SDK 的资源?
我对如何显示 floorPlan 和获取当前位置感到困惑。
请帮助我。
大致方法如下:
1) 初始化 IndoorAtlas 实例:
IndoorAtlas ia = IndoorAtlasFactory.createIndoorAtlas(context, listener, apiKey, apiSecret);
2) 获取FloorPlan实例:
FutureResult<FloorPlan> result = ia.fetchFloorPlan(floorPlanId);
result.setCallback(new ResultCallback<FloorPlan>() {
@Override
public void onResult(final FloorPlan result) {
mFloorPlan = result;
loadFloorPlanImage(result);
}
// handle error conditions too
}
3) 获取实际图像:
void loadFloorPlanImage(FloorPlan floorPlan) {
BitmapFactory.Options options = createBitmapOptions(floorPlan);
FutureResult<Bitmap> result = ia.fetchFloorPlanImage(floorPlan, options);
result.setCallback(new ResultCallback<Bitmap>() {
@Override
public void onResult(final Bitmap result) {
// now you have floor plan bitmap, do something with it
updateImageViewInUiThread(result);
}
// handle error conditions too
}
}
4)开始定位:
ia.startPositioning(venueId, floorId, floorPlanId);
5) 在平面图上显示位置:
public void onServiceUpdate(ServiceState state) {
// get position on original floor plan image
int i = state.getImagePoint().getI();
int j = state.getImagePoint().getJ();
// take into account how your floor plan image has been scaled
// and draw position
PointF scaledPoint = new PointF();
Util.calculateScaledPoint((int) floorPlan.dimensions[0], (int) floorPlan.dimensions[1], i, j, mImageView, scaledPoint);
drawNewPositionInUiThread(scaledPoint.x, scaledPoint.y);
}
当然可以先定位再获取图像。您也可以在本地缓存图像,但如前所述,大致就是这样。
Util.java:
public class Utils {
/**
* Calculates scaling factor for an image with original dimensions of
* {@code originalWidth x originalHeight} being displayed with {@code imageView}.
*
* The assumption with this example code is that a) layout has been already performed for
* {@code imageView} and that {@link android.widget.ImageView.ScaleType#CENTER_INSIDE} is used.
*
* @param originalWidth height of the original bitmap to be displayed using {@code imageView}
* @param originalHeight width of the original bitmap to be displayed using {@code imageView}
*/
public static float calculateScaleFactor(int originalWidth, int originalHeight,
ImageView imageView) {
if (imageView.getScaleType() != ImageView.ScaleType.CENTER_INSIDE) {
throw new IllegalArgumentException("only scale type of CENTER_INSIDE supported, was: "
+ imageView.getScaleType());
}
final int availableX = imageView.getWidth()
- (imageView.getPaddingLeft() + imageView.getPaddingRight());
final int availableY = imageView.getHeight()
- (imageView.getPaddingTop() + imageView.getPaddingBottom());
if (originalWidth > availableX || originalHeight > availableY) {
// original image would not fit without scaling
return originalWidth > availableX
? availableX / (float) originalWidth
: availableY / (float) originalHeight;
} else {
return 1f; // no scaling required
}
}
/**
* Calculates point where to draw coordinates {@code x} and {@code y} in a bitmap that's
* original dimensions were {@code originalWidth x originalHeight} and may now be scaled down
* as it's been displayed with {@code imageView}.
*
* @param originalWidth width of the original bitmap before any scaling
* @param originalHeight height of the original bitmap before any scaling
* @param x x-coordinate on original bitmap
* @param y y-coordinate on original bitmap
* @param imageView view that will be used to display bitmap
* @param point point where result value is to be stored
* @see #calculateScaleFactor(int, int, ImageView)
*/
public static void calculateScaledPoint(int originalWidth, int originalHeight,
int x, int y,
ImageView imageView,
PointF point) {
final float scale = calculateScaleFactor(originalWidth, originalHeight, imageView);
final float scaledWidth = originalWidth * scale;
final float scaledHeight = originalHeight * scale;
// when image inside view is smaller than the view itself and image is centered (assumption)
// there will be some empty space around the image (here offset)
final float offsetX = Math.max(0, (imageView.getWidth() - scaledWidth) / 2);
final float offsetY = Math.max(0, (imageView.getHeight() - scaledHeight) / 2);
point.x = offsetX + (x * scale);
point.y = offsetY + (y * scale);
}
}