我想在全屏项目中创建一个谷歌地图片段,然后当我触摸谷歌地图片段时调用一些函数,但当我触摸谷歌地图片段集时,点击侦听器不起作用这是我的XML部分和Java部分
<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="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
/>
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
mVisible = true;
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.map);
// Set up the user interaction to manually show or hide the system UI.
mContentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggle();
}
});
// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
}
创建 xml 布局文件:
content_main.xml包含 MapFragment,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.journaldev.MapsInAction.MainActivity"
tools:showIn="@layout/activity_main">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="match_parent"
/>
</RelativeLayout>
主活动.java定义如下:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
SupportMapFragment mapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.4233438, -122.0728817))
.title("LinkedIn")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.4629101,-122.2449094))
.title("Facebook")
.snippet("Facebook HQ: Menlo Park"));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.3092293, -122.1136845))
.title("Apple"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.4233438, -122.0728817), 10));
}
});
}
});
}
@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_main, 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(37.4233438, -122.0728817))
.title("LinkedIn")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.4629101,-122.2449094))
.title("Facebook")
.snippet("Facebook HQ: Menlo Park"));
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(37.3092293, -122.1136845))
.title("Apple"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.4233438, -122.0728817), 10));
}
}
如果你不明白。请访问此链接。 http://www.journaldev.com/10380/android-google-maps-in-action-example-tutorial
祝你好运!
我认为这里的想法是他想让地图片段像视图一样,其中片段用作地图查看器,他想使其可点击,因此点击操作将调用他在其中实现的代码。
我之前遇到了这个问题,我意识到片段不能用作视图,因为它里面的材料是应该像视图而不是碎片一样处理的材料。但是对于片段中的地图,有一个技巧可以使其看起来像"视图">
首先,您需要设置地图的片段:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后在保存片段的片段或活动类中,
class FormFragment : Fragment(), OnMapReadyCallback, GoogleMap.OnMapClickListener{
lateinit var binding: FragmentAddplaceFormBinding
lateinit var map: GoogleMap
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentAddplaceFormBinding.inflate(inflater, container, false)
binding.map.onCreate(savedInstanceState)
binding.map.onResume()
try{
activity?.let { MapsInitializer.initialize(it.applicationContext) }
}catch (e: Exception){
e.printStackTrace()
}
binding.map.getMapAsync(this)
return binding.root
}
正如你所注意到的,这个类实现了GoogleMap.OnMapClickListener,它将用于使"可点击"。
你应该能够实现成员,你会得到这些
// This is the one that will do the trick
override fun onMapClick(p0: LatLng) {
// Your code here to make it look like the map is clicked on touch
}
override fun onMapReady(p0: GoogleMap) {
}
然后像这样做
override fun onMapClick(p0: LatLng) {
Toast.makeText(context, "You clicked on the map, Congrats!", Toast.LENGTH_SHORT).show()
}
override fun onMapReady(p0: GoogleMap) {
map = p0
p0.setOnMapClickListener(this)
// These settings can be changed to whatever you desired,
// but because I want to make it look like a decend Map Viewer,
// so I disable all of these
with(map.uiSettings){
isZoomControlsEnabled = false
isCompassEnabled = false
isMyLocationButtonEnabled = false
isZoomGesturesEnabled = false
isScrollGesturesEnabled = false
isRotateGesturesEnabled = false
isTiltGesturesEnabled = false
}
}
希望这能有所帮助