[英]I can't figure out how to use this MapView Library
I've been trying to create an indoor map with a Zoom functionality.I found the library "MapView" ( https://github.com/peterLaurence/MapView ) which is exactly what I would need to achieve what I want.我一直在尝试创建具有缩放功能的室内地图。我找到了库“MapView”( https://github.com/peterLaurence/MapView ),这正是实现我想要的目标所需要的。 Sadly all of the documentation is in Kotlin and I have no knowledge of Kotlin.
遗憾的是,所有文档都在 Kotlin 中,而我对 Kotlin 一无所知。
I tried to make a simple programm to start with.我试图制作一个简单的程序来开始。 I wanted to simply show the map on the screen but it doesn't get drawn at all.
我想简单地在屏幕上显示地图,但根本没有绘制。 I don't get any errors either so I don't really know what I can do.
我也没有收到任何错误,所以我真的不知道我能做什么。
Here is my code这是我的代码
public class MainActivity extends AppCompatActivity {
private MapView mapView;
private TileStreamProvider tileStreamProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView= findViewById(R.id.view);
mapView = new MapView(this);
tileStreamProvider = new TileStreamProvider() {
@Nullable
@Override
public InputStream getTileStream(int row, int col, int zoomLvl) {
InputStream inputStream = null;
AssetManager assetManager = MainActivity.this.getAssets();
if (inputStream == null) {
try {
assetManager.open("tiles/esp/" + zoomLvl + '/' + row + '/' + col + ".jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
return inputStream;
}
};
MapViewConfiguration config = new MapViewConfiguration(1, 8192, 8192, 256, tileStreamProvider);
mapView.configure(config);
}
}
Any help would be appreciated !
EDIT编辑
I tried to do it another way, this time in a fragment and not directly on the main Activity by using the demo that is provided as example.我尝试以另一种方式进行,这次是在片段中,而不是通过使用作为示例提供的演示直接在主 Activity 上进行。 But again since I don't really understand kotlin I may or may not have written soöe stuff the wrong way because I still end up with a white screen in the end.
但同样,由于我不太了解 kotlin,我可能会或可能不会以错误的方式编写 soöe 的东西,因为我最终还是以白屏告终。
Here is my code now这是我现在的代码
public class ExampleFragment extends Fragment {
private ViewGroup parentView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
parentView = container;
MapView mapView = makeMapView(getContext());
mapView.setId(R.id.mapview_id);
mapView.setSaveEnabled(true);
parentView.addView(mapView, 0);
View v = inflater.inflate(R.layout.example_fragment, parentView, false);
return v;
}
private MapView makeMapView(Context context) {
TileStreamProvider tileStreamProvider = (row, col, zoomLvl) -> {
try {
context.getAssets().open("tiles/esp/" + zoomLvl + '/' + row + '/' + col + ".jpg");
} catch (IOException e) {
e.printStackTrace();
}
return null;
};
int tileSize = 256;
MapViewConfiguration configuration = new MapViewConfiguration(5, 8192, 8192, tileSize, tileStreamProvider).setMaxScale(2f);
MapView mapView = new MapView(context);
mapView.configure(configuration);
return mapView;
}
}
SECOND EDIT第二次编辑
The code for my fragment looks like this now我的片段的代码现在看起来像这样
public class ExampleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ViewGroup v = (ViewGroup) inflater.inflate(R.layout.example_fragment, container, false);
MapView mapView = makeMapView(getContext());
mapView.setId(R.id.mapview_id);
mapView.setSaveEnabled(true);
v.addView(mapView);
return v;
}
private MapView makeMapView(Context context) {
TileStreamProvider tileStreamProvider = (row, col, zoomLvl) -> {
try {
context.getAssets().open("tiles/esp/" + zoomLvl + '/' + row + '/' + col + ".jpg");
} catch (IOException e) {
e.printStackTrace();
}
return null;
};
int tileSize = 256;
MapViewConfiguration configuration = new MapViewConfiguration(5, 8192, 8192, tileSize, tileStreamProvider).setMaxScale(2f);
MapView mapView = new MapView(context);
mapView.configure(configuration);
return mapView;
}
}
but it still doesn't work
You need to add your instance of MapView into one of your view groups.您需要将 MapView 实例添加到您的视图组之一。
Assuming you have a linear layout with the id R.id.main
in your activity:假设您的活动中有一个带有 id
R.id.main
的线性布局:
LinearLayout mLayout = findViewById(R.id.linear_layout);
MapView mapView = new MapView(context);
mLayout.addView(mapView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.