2017-04-18 52 views
0

我正在處理需要室內地圖的Android應用項目。我使用的位置在Google地圖上有可用的地板。我使用Google Maps Android API。我在顯示的初始級別上設置了標記。當我更換地板時,如何移除/隱藏這些標記並將其替換爲新標記? 這裏是我的Java代碼:谷歌地圖Android API切換樓層時刪除標記

package com.me.maps; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

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.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @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); 
    } 



    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Milwaukee, Wisconsin. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng mpm = new LatLng(43.0408468, -87.921474); 
     LatLng planetarium = new LatLng(43.040622,-87.920798); 
     LatLng theater = new LatLng(43.040497,-87.920640); 
     LatLng marketplace = new LatLng(43.040779,-87.921717); 
     mMap.addMarker(new MarkerOptions().position(mpm).title("Milwaukee Public Museum")); 
     mMap.addMarker(new MarkerOptions().position(planetarium).title("The Daniel M. Soref National Geographic Planetarium")); 
     mMap.addMarker(new MarkerOptions().position(theater).title("The Daniel M. Soref National Geographic Theater")); 
     mMap.addMarker(new MarkerOptions().position(marketplace).title("Museum Marketplace")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mpm, 18)); 
    } 
} 

回答

0
//Place current location marker 
    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title("Current Position"); 
    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.doctor); 
    markerOptions.icon(icon); 
    mCurrLocationMarker = mMap.addMarker(markerOptions); 

    //move map camera 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

使用此代碼用於改變標記圖標。

+0

感謝您的回覆。我不確定這是怎麼回事。我將它添加到onMapReady()並顯示錯誤。有沒有一種方法來實現這一點?這是否需要連接到樓層選擇器按鈕? –

+0

當您單擊樓層選擇器按鈕時,只需使用@here清除已創建的標記,'''markerOptions.clear()''' –

0

當您切換樓層時,您的應用應該清除地圖上已有的標記。使用Google地圖實例的.clear()方法,然後僅添加與該樓層相關的標記。

此方法的文檔,可以發現在:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap

希望它幫助。

+0

我無法找到將.clear()附加到的位置。我正在查看文檔,對樓層選擇器的唯一引用是:setIndoorLevelPickerEnabled(true);是他們的事件監聽器還是我需要添加到我的代碼中的東西?我不知道我應該在哪裏看。我沒有看到有關如何添加任何內容來點擊這些特定按鈕的任何參考。 –