2013-02-23 155 views

回答

3

據我所知,你不能改變羅盤的位置,但您可以禁用它,打造你的私人之一。

See example here

+0

+1有趣。讓我們看看是否有人提出瞭解決方法,如果不是,那麼信用就是你的。 – capdragon 2013-02-23 18:33:51

+0

但是這是使用MapView。任何想法與MapFragment? – 2013-09-05 20:31:15

1

我知道這是一個漫長的時間,這個問題被問,但我前幾天菲爾德在這個問題上我修正了這樣的問題:

try { 
       assert mapFragment.getView() != null; 
       final ViewGroup parent = (ViewGroup) mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton").getParent(); 
       parent.post(new Runnable() { 
        @Override 
        public void run() { 
         try { 
          for (int i = 0, n = parent.getChildCount(); i < n; i++) { 
           View view = parent.getChildAt(i); 
           RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
           // position on right bottom 
           rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 
           rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0); 
           rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
           rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
           rlp.rightMargin = rlp.leftMargin; 
           rlp.bottomMargin = 25; 
           view.requestLayout(); 
          } 
         } catch (Exception ex) { 
          ex.printStackTrace(); 
         } 
        } 
       }); 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

在這個例子中,我把指南針放在角落右邊。您需要確保在執行此操作時創建了mapFragment,我建議您在MapFragment的「onMapReady」方法中運行代碼。

+1

這是放置在左側rof某些原因! – Paschalis 2017-02-05 09:11:15

7
@Override 
public void onMapReady(GoogleMap map) { 

    try { 
     final ViewGroup parent = (ViewGroup) mMapView.findViewWithTag("GoogleMapMyLocationButton").getParent(); 
     parent.post(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Resources r = getResources(); 
        //convert our dp margin into pixels 
        int marginPixels = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics()); 
        // Get the map compass view 
        View mapCompass = parent.getChildAt(4); 

        // create layoutParams, giving it our wanted width and height(important, by default the width is "match parent") 
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(mapCompass.getHeight(),mapCompass.getHeight()); 
        // position on top right 
        rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 
        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
        rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0); 
        //give compass margin 
        rlp.setMargins(marginPixels, marginPixels, marginPixels, marginPixels); 
        mapCompass.setLayoutParams(rlp); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     }); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 

} 
0

基於填充的解決方案對於小偏移工作,但據我知道有沒有公開的API,使我們能夠穩健地改變指南針的水平「萬有引力」由左到右像股票谷歌地圖應用作用:

enter image description here

注意,如果你採用了大量的左填充的指南針向右移動在自己的應用程序,在左下角的谷歌標誌也將被轉移到右側。

0

在2.0地圖api上。

 // change compass position 
    if (mapView != null && 
      mapView.findViewById(Integer.parseInt("1")) != null) { 
     // Get the view 
     View locationCompass = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("5")); 
     // and next place it, on bottom right (as Google Maps app) 
     RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) 
       locationCompass.getLayoutParams(); 
     // position on right bottom 
     layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
     layoutParams.setMargins(0, 160,30, 0); // 160 la truc y , 30 la truc x 
    }