2016-02-09 94 views
1

因此,基本上我試圖把一個按鈕放在我的地圖片段頂部,但它似乎不工作,儘管地圖工作完全正常。即使廣泛搜索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

使用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> 

enter image description here

+0

我已經編輯了答案,包括你建議的框架佈局。但是答案對我沒有好處。那麼,我注意到的另一件事是,我沒有在我的Java文件中使用oncreateview方法。因此,XML不會被誇大。因此,沿着這條線回答可能會有幫助。 – rohanagarwal94