所以,基本上我试图在我的地图片段顶部放置一个按钮,但它似乎无法工作,尽管地图工作完全正常。即使在广泛搜索Google之后,我也找不到可执行的答案。
这是我的地图片段 -
public class Tab1 extends SupportMapFragment implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
GoogleMap.OnInfoWindowClickListener, OnMapReadyCallback,
ClusterManager.OnClusterClickListener<MyItem>,
ClusterManager.OnClusterInfoWindowClickListener<MyItem>,
ClusterManager.OnClusterItemClickListener<MyItem> {
InputStream inputstream;
BufferedReader reader;
String title;
GoogleMap map;
AutoCompleteTextView actv;
Map<String, String> moviemap;
List<MyItem> items = new ArrayList<MyItem>();
String subtitle;
String floor;
String type;
Double lat;
Double lng;
int len;
int arraylength;
ClusterManager<MyItem> mClusterManager;
List<Map<String, String>> moviedata;
JSONArray jsonArray;
JSONObject jsonObject;
String m;
private GoogleApiClient mGoogleApiClient;
private Location mCurrentLocation;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mClusterManager = new ClusterManager<MyItem>(getActivity(), getMap());
getMap().setOnCameraChangeListener(mClusterManager);
getMap().setOnMarkerClickListener(mClusterManager);
mClusterManager
.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
@Override
public boolean onClusterClick(final Cluster<MyItem> cluster) {
getMap().animateCamera(CameraUpdateFactory.newLatLngZoom(
cluster.getPosition(), (float) Math.floor(getMap()
.getCameraPosition().zoom + 2)), 300,
null);
return true;
}
});
try {
inputstream = getResources().getAssets().open("map.json");
reader = new BufferedReader(new InputStreamReader(inputstream));
m = reader.toString();
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
m = total.toString();
} catch (IOException ex) {
Toast.makeText(getActivity(), "fail", Toast.LENGTH_SHORT).show();
}
moviedata = new ArrayList<Map<String, String>>();
try {
jsonObject = new JSONObject(m);
jsonArray = jsonObject.optJSONArray("map");
arraylength = jsonArray.length();
len = arraylength;
for (int i = 0; i < arraylength; i++) {
moviemap = new HashMap<>();
JSONObject jsonChildNode = jsonArray.getJSONObject(i);
title = jsonChildNode.optString("title");
subtitle = jsonChildNode.optString("subtitle");
floor = jsonChildNode.optString("floor");
type = jsonChildNode.optString("type");
lat = jsonChildNode.optDouble("latitude");
lng = jsonChildNode.optDouble("longitude");
moviemap.put("A", title);
moviemap.put("B", subtitle);
moviemap.put("C", floor);
moviemap.put("D", type);
moviemap.put("E", lat.toString());
moviemap.put("F", lng.toString());
moviedata.add(moviemap);
Double lat1 = Double.parseDouble(moviedata.get(i).get("E"));
Double lng1 = Double.parseDouble(moviedata.get(i).get("F"));
items.add(new MyItem(lat1, lng1, moviedata.get(i).get("A"), moviedata.get(i).get("B")));
}
mClusterManager.addItems(items);
mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), getMap(), mClusterManager));
} catch (JSONException e) {
e.printStackTrace();
}
initListeners();
}
private void initListeners() {
getMap().setOnInfoWindowClickListener(this);
}
private void removeListeners() {
if (getMap() != null) {
getMap().setOnInfoWindowClickListener(null);
}
}
@Override
public void onDestroy() {
super.onDestroy();
removeListeners();
}
private void initCamera(Location location) {
CameraPosition position = CameraPosition.builder()
.target(new LatLng(28.75007207311156, 77.11772996932268))
.zoom(18f)
.bearing(0.0f)
.tilt(40f)
.build();
getMap().animateCamera(CameraUpdateFactory.newCameraPosition(position), null);
getMap().setMapType(GoogleMap.MAP_TYPE_NORMAL);
getMap().setTrafficEnabled(true);
getMap().setMyLocationEnabled(true);
getMap().getUiSettings().setZoomControlsEnabled(true);
}
@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
initCamera(mCurrentLocation);
}
@Override
public void onConnectionSuspended(int i) {
//handle play services disconnecting if location is being constantly used
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Create a default location if the Google API Client fails. Placing location at Googleplex
mCurrentLocation = new Location("");
mCurrentLocation.setLatitude(28.7499);
mCurrentLocation.setLongitude(77.1170);
initCamera(mCurrentLocation);
}
@Override
public void onInfoWindowClick(Marker marker) {
}
@Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
return false;
}
@Override
public void onClusterInfoWindowClick(Cluster<MyItem> cluster) {
}
@Override
public boolean onClusterItemClick(MyItem myItem) {
return false;
}
}
这是我的xml -
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center" />
</FrameLayout>
答案 0 :(得分:0)
使用FrameLayout
代替RelativeLayout
,如下所示。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="left|top" />
</FrameLayout>