Java 将谷歌地图显示为libgdx游戏中的片段
我会尽快完成,希望有人仍然使用libgdx(关于这个主题的所有教程都已经过时了) 我现在想做的是创建一个谷歌地图,然后将该地图插入到我的libgdx游戏中,这样我就可以使用用户在地图上的当前位置作为游戏地图 为此,我在我的核心项目中创建了一个接口Java 将谷歌地图显示为libgdx游戏中的片段,java,android,libgdx,Java,Android,Libgdx,我会尽快完成,希望有人仍然使用libgdx(关于这个主题的所有教程都已经过时了) 我现在想做的是创建一个谷歌地图,然后将该地图插入到我的libgdx游戏中,这样我就可以使用用户在地图上的当前位置作为游戏地图 为此,我在我的核心项目中创建了一个接口 public interface MapInterface { // Testmethod int showMeTheMoney(); } 我给我的游戏一个构造函数,它把这个接口作为参数保存在一个变量中 public class AndroidLau
public interface MapInterface {
// Testmethod
int showMeTheMoney();
}
我给我的游戏一个构造函数,它把这个接口作为参数保存在一个变量中
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);
}
}
然后我初始化游戏,给游戏一个我的地图的新实例。我可以使用界面功能;地图确实存在,或者至少存在允许我使用函数的对象
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>
现在,我完全不知道如何显示这张该死的地图。如何在游戏中显示地图片段?首先,您需要创建显示地图的界面:
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();方法,无论何时需要打开活动
注意:如果您只想使用用户的当前位置,则应使用谷歌地图获取gps位置。嘿,谢谢您的回答。这段代码的问题是,我想在地图上显示玩家的位置,计算怪物穿越的路线,然后在地图上放置炮塔。不过,我确实使用了您的确切代码从胜利/失败屏幕返回到主要活动,因此它确实有效:)此后,我通过谷歌地图提供的onSnapShotReadyCallback,在用户按下“开始游戏”时拍摄谷歌地图的快照,从而解决了这个问题。我只需将路径保存到位图,为游戏提供路径,并将其加载为背景纹理。抱歉,重复评论:然后我还获取地图快照的“边界”,将gps坐标转换为像素坐标,然后使用该坐标定位所有内容。我已经使用了routes API,但我仍然需要将其转换为屏幕坐标,因为在libgdx游戏中使用实际地图而不是快照似乎非常复杂。目前,GPS边界允许我轻松转换坐标系,位置服务会在我需要时为我提供当前位置。现在似乎没有必要再讨论这个问题。