2013-02-23 157 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 
    }