如何解决我不知道如何使用此MapView库
我一直在尝试创建具有缩放功能的室内地图。我发现了图书馆“ MapView”(https://github.com/peterLaurence/MapView),这正是我要实现所需的功能。可悲的是,所有文档都在Kotlin中,而我对Kotlin一无所知。
我试图做一个简单的程序。我只想在屏幕上显示地图,但根本无法绘制。我也没有任何错误,所以我真的不知道该怎么办。
这是我的代码
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,256,tileStreamProvider);
mapView.configure(config);
}
}
Any help would be appreciated !
编辑
我尝试使用另一种方式进行操作,这一次是使用一个示例,而不是直接在主要Activity上,而是在一个片段中进行。但是又一次,因为我不太了解科特林,所以我可能写错了东西,也可能写错了东西,因为最后我仍然看到白屏。
这是我的代码
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,tileSize,tileStreamProvider).setMaxScale(2f);
MapView mapView = new MapView(context);
mapView.configure(configuration);
return mapView;
}
}
第二编辑
我片段的代码现在看起来像这样
public class ExampleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@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,tileStreamProvider).setMaxScale(2f);
MapView mapView = new MapView(context);
mapView.configure(configuration);
return mapView;
}
}
but it still doesn't work
解决方法
您需要将MapView实例添加到您的一个视图组中。
假设您在活动中使用ID为R.id.main
的线性布局:
LinearLayout mLayout = findViewById(R.id.linear_layout);
MapView mapView = new MapView(context);
mLayout.addView(mapView);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。