我会尽快完成,希望有人还在使用 libgdx(所有关于该主题的教程都已经过时了)
我现在想做的是创建一个 GoogleMap,然后将该 map 插入我的 libgdx 游戏中,这样我就可以将用户在 map 上的当前位置用作游戏 map 。
为此,我在我的核心项目中创建了一个接口(interface)。
public interface MapInterface {
// Testmethod
int showMeTheMoney();
}
我为我的游戏提供了一个构造函数,它将此接口(interface)作为参数并将其保存在一个变量中。
public class AndroidLauncher extends AndroidApplication {
MyGdxGame gdx;
AndroidApplicationConfiguration cfg;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cfg = new AndroidApplicationConfiguration();
gdx = new MyGdxGame(new MapsActivity());
initialize(gdx, cfg);
}
}
然后我初始化游戏,为游戏提供我的 map 的新实例。我可以使用接口(interface)函数;该 map 确实存在,或者至少存在一个允许我使用函数的对象。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, MapInterface {
private GoogleMap mMap;
public void onClick(View v){
Intent intent = new Intent(this, AndroidLauncher.class);
//To pass:
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public int showMeTheMoney() {
return 0;
}
}
和 MapsActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="500px"
android:layout_height="500px"
tools:context="com.mygdx.game.android.MapsActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Start Game" />
</LinearLayout>
现在,我完全不知道如何显示该死的 map 。如何在我的游戏中显示 MapFragment?
最佳答案
首先您需要创建将显示您的 map 的界面:
public interface MapsInterface {
void openMaps();
}
现在你应该在你的 android 应用程序上实现它并将它传递给你的 gdxgame:
public class AndroidLauncher extends AndroidApplication implements MapsInterface{
MyGdxGame gdx;
AndroidApplicationConfiguration cfg;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cfg = new AndroidApplicationConfiguration();
gdx = new MyGdxGame(this);
initialize(gdx, cfg);
}
public void openMaps(){
Intent i = new Intent(getApplicationContext(), MapsActivity.class);
startActivity(i);
}
}
现在在您的游戏中,您只需获取 MapsInterface 引用并调用 openMaps();每当您需要打开 Activity 时的方法。
注意:如果你只想使用用户的当前位置,你应该去获取 gps 位置而不是使用谷歌地图。
关于java - 在 libgdx 游戏中将 GoogleMap 显示为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904141/