android-maps-v2 - map v2 我的位置图层样式

标签 android-maps-v2

如何更改我的位置图层的基本蓝色。 我可以用记号笔替换它吗?或者我必须 build 定制瓷砖。

我可以只删除带有半径的销钉并保留“定位我”图 block 吗

最佳答案

我没有找到任何样式定制。并且标记无法更换。

我做了一个自定义磁贴和一个自定义 getCurrentLocation 函数。

这是我的代码:

首先,您需要右上角的“定位我”按钮。我将其添加到布局中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="android.support.v4.app.FragmentActivity" >

    <fragment
        android:id="@+id/tabs_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.bucons.savetime.MyMapFragment" />

    <ImageButton
        android:id="@+id/myLocationButton"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:padding="4dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp" 
        android:scaleType="fitCenter" 
        android:src="@drawable/ic_menu_mylocation"
        android:background="@drawable/map_tile_bg" />
</RelativeLayout>

所需的资源是@drawable/ic_menu_mylocation 您可以从以下位置获取:https://docs.google.com/file/d/0BzGos46YSzYfWkRYS2ZMNmdZREk/edit

背景:@drawable/map_tile_bg:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
          android:exitFadeDuration="@android:integer/config_shortAnimTime">
     <item android:state_pressed="true">
         <shape android:shape="rectangle">
             <stroke 
                 android:width="1px"
                 android:color="@color/map_tile_bg_stroke_pressed"/>
             <solid android:color="@color/map_tile_bg_solid_pressed"/>
             <corners android:radius="1dp"/>
         </shape>
     </item>
       <item>
         <shape android:shape="rectangle">
             <stroke 
                 android:width="1px" 
                 android:color="@color/map_tile_bg_stroke"/>
             <solid android:color="@color/map_tile_bg_solid"/>
             <corners android:radius="1dp"/>
         </shape>
       </item>
</selector>

颜色资源是:

<color name="map_tile_bg_solid">#aaffffff</color>
<color name="map_tile_bg_stroke">#ffcccccc</color>
<color name="map_tile_bg_solid_pressed">#aaFF9933</color>
<color name="map_tile_bg_stroke_pressed">#ffcccccc</color>

这是我的片段:

public class MyMapFragment extends SupportMapFragment {

    //Current location Marker
    private Marker myLocationMarker;

    GoogleMap map = null;

    public MyMapFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setUpMapIfNeeded();
        getCurrentLocation();
    }  

    //set the map and set the tile for locate me button
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (map == null) {
            map = getMap();
            // Check if we were successful in obtaining the map.
            if (map != null) {
                //Top right button for go to location
                ImageButton locateMeTile = (ImageButton) getSherlockActivity().findViewById(R.id.myLocationButton);
                locateMeTile.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getCurrentLocation();
                    }
                });
            }
        }
    }

    //retrieve the current position. Only once
    private void getCurrentLocation() {
        locationManager = (LocationManager) getSherlockActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria crit = new Criteria();
        String towers = locationManager.getBestProvider(crit, false);

        Location location = locationManager.getLastKnownLocation(towers);

        Double glat = null;
        Double glon = null;
        if(location != null){

            glat = location.getLatitude();
            glon = location.getLongitude();

        }

        CameraPosition pos = new CameraPosition.Builder()
        .target(new LatLng(glat, glon))
        .zoom(10)
        .build();     

        if(pos != null) {
            currLocationChange(pos.target);
            map.animateCamera(CameraUpdateFactory.newCameraPosition(pos));
        } else {
            Toast.makeText(getSherlockActivity(), R.string.main_error, Toast.LENGTH_LONG).show();
        }
    }

    //Add pin to the current Location or move existing
    private void currLocationChange(LatLng loc) {
        if(myLocationMarker != null) {
            myLocationMarker.setPosition(new LatLng(loc.latitude,loc.longitude));
        } else {
            myLocationMarker = map.addMarker(new MarkerOptions()
                    .position(new LatLng(loc.latitude,loc.longitude)));
        }
    }
}

关于android-maps-v2 - map v2 我的位置图层样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316355/

相关文章:

android - PolylineOptions .color() 不工作

安卓谷歌地图API V2 : Open Custom Info Window on Right Side of Marker

带标签的 Android 谷歌地图标记?

android - 检查 Point 是否位于(或靠近)凸多边形边缘

android - 带有 fragment 的重复 ID

java - 谷歌地图API v2在多个 fragment android

android-maps-v2 - 在 Android 中设置 Google map 样式

android - 如何实时更新标记位置

android - 扩展 MapFragment Android

Android - 如果 View 不可见,则在 RecyclerViewAdapter 中添加 MapFragment 失败