gpt4 book ai didi

java - 在 Android map 应用程序中更改 map 类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:01 25 4
gpt4 key购买 nike

我是编程新手。我正在上移动开发类(class),这对我来说是全新的。我在 Android Studio 中创建了一个非常基本的 Android 应用程序,它显示了一个标准的谷歌地图。我需要在应用程序上创建按钮以将 map 从普通 map 更改为卫星 map 、地形 map 和混合 map 。我一直在这里和其他多个来源进行搜索,但我不知道从哪里开始,或者将我在其他站点上找到的代码放在哪里。谁能帮忙?我敢肯定这很容易,我只是不知道该怎么做。

谢谢!

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity"
tools:ignore="MissingPrefix">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_margin="5dp"
class="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="false"
map:uiTiltGestures="true"
map:uiZoomControls="true"
map:uiZoomGestures="true"
/>
</RelativeLayout>

.java文件

package geog498v.mymap;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements OnMapReadyCallback {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setScrollGesturesEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
}

}

最佳答案

要更改 map 类型,请使用 this在你的 onMapReady() 里面。

 googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

关于java - 在 Android map 应用程序中更改 map 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com