在libgdx游戏中将GoogleMap显示为片段

时间:2018-06-18 07:01:06

标签: java android libgdx

我会快速做到这一点,希望有人甚至还使用libgdx(关于这个主题的所有教程都是搞笑的过时)

我现在要做的是创建一个GoogleMap,然后在我的libgdx游戏中插入该地图,这样我就可以将地图上用户的当前位置用作游戏地图。

为此我在Core-Project中创建了一个接口。

public interface MapInterface {
// Testmethod
int showMeTheMoney();
}

我给我的Game一个构造函数,它将此接口作为参数并将其保存在变量中。

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实例。我可以使用界面功能;地图确实存在,或者至少存在允许我使用函数的对象。

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>

现在,我完全不知道如何展示该死的地图。如何在游戏中显示MapFragment?

1 个答案:

答案 0 :(得分:1)

首先,您需要创建显示地图的界面:

string path = Directory.GetCurrentDirectory() + @"\people2.xml";
XDocument document = XDocument.Load(path);
FileStream fss = new FileStream(path, FileMode.Open, FileAccess.Write);

现在您应该在android应用程序上实现它,并将其传递给gdxgame:

public interface MapsInterface {
    void openMaps();
}

现在在您的游戏中,您只需获取MapsInterface引用并调用openMaps();即可。方法,只要您需要打开活动即可。

注意:如果您只想使用用户的当前位置,则应该去获取gps位置,而不要使用Google地图。