如何使用android studio

时间:2016-04-22 05:28:25

标签: java android html google-maps android-studio

我正在制作谷歌地图,我需要更改“我的位置”(当前位置按钮)的位置。目前,当前位置按钮位于右上方。所以请帮我如何在谷歌地图屏幕上重新定位当前位置按钮。提前谢谢。

我的示例代码:

final GoogleMap googleMap = mMapView.getMap();
googleMap.setMyLocationEnabled(true);

13 个答案:

答案 0 :(得分:18)

我在地图片段中通过使用下面的代码将我的位置按钮重新定位到视图的右下角解决了这个问题,这是我的 MapsActivity.java: -

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

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;
    View mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_map);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapView = mapFragment.getView();
        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 Sydney, Australia.
     * 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;
        mMap.setMyLocationEnabled(true);

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

        if (mapView != null &&
                mapView.findViewById(Integer.parseInt("1")) != null) {
            // Get the button view
            View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            // and next place it, on bottom right (as Google Maps app)
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                    locationButton.getLayoutParams();
            // position on right bottom
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 30, 30);
        }

    }
}

这是片段的布局: -

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.infogird.www.location_button_reposition.MapFragment">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</FrameLayout>

我希望,这将解决您的问题。感谢。

答案 1 :(得分:14)

你可以用这个

View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 180, 180, 0);

答案 2 :(得分:6)

Kotlin版本接受的答案(右下角)(因为autoconvert失败并带有此代码)

 val locationButton= (mapView.findViewById<View>(Integer.parseInt("1")).parent as View).findViewById<View>(Integer.parseInt("2"))
 val rlp=locationButton.layoutParams as (RelativeLayout.LayoutParams)
 // position on right bottom
 rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP,0)
 rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE)
 rlp.setMargins(0,0,30,30);

答案 3 :(得分:6)

已接受答案的MapFragment的Kotlin版本

val locationButton = (mapFragment.view?.findViewById<View>(Integer.parseInt("1"))?.parent as View).findViewById<View>(Integer.parseInt("2"))
                    val rlp =  locationButton.getLayoutParams() as RelativeLayout.LayoutParams
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0)
                    rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE)
                    rlp.setMargins(0, 0, 30, 30)

答案 4 :(得分:5)

您可以在以下所有条件下使用以下代码,例如: 1.如果你想在右上角显示位置按钮 2.如果你想在右下方显示位置按钮 3.如果你想在左下角显示位置按钮

1.如果您想在右上角显示位置按钮

View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

2.如果您想在右下方显示位置按钮

 View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);rlp.setMargins(0,0,30,30);

3.如果您想在左下角显示位置按钮

 View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            // position on right bottom


            rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

             rlp.addRule(RelativeLayout.ALIGN_PARENT_END, 0);
            rlp.addRule(RelativeLayout.ALIGN_END, 0);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            rlp.setMargins(30, 0, 0, 40);

最好的运气

答案 5 :(得分:2)

通过将填充设置为GoogleMap Fragment,您可以更改 myLocationButton 的位置,但唯一的问题是它还会更改其他按钮的位置,例如缩放。

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

@Override
public void onMapReady(GoogleMap map) {  
    map.setPadding(left, top, right, bottom);
}

答案 6 :(得分:1)

它的工作在MapView上显示我的位置按钮右下角

  

XML

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  

JAVA

private MapView mMapView;

mMapView = (MapView) findViewById(R.id.mapView);

mMapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(final GoogleMap mMap) {
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        }

        if (mMapView != null && mMapView.findViewById(Integer.parseInt("1")) != null) {
            View locationButton = ((View) mMapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            layoutParams.setMargins(0, 0, 20, 20);
        }
    }
});

答案 7 :(得分:0)

你可以做一件事。您禁用默认&#34;我的位置&#34;按钮并在您的愿望位置创建一个自定义按钮,点击该按钮即可将camara移动到当前的latlng。

答案 8 :(得分:0)

不幸的是,你只能设置填充,如下所示:

@Override
public void onMapReady(GoogleMap googleMap) {
   googleMap.setPadding(left, top, right, bottom);
   ...
}

我最终创建了自己的内容并使用layout_gravity将其嵌入到框架布局中以指定我想要的位置,在本例中为bottom / end:

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="300dp">

   <fragment
       android:id="@+id/map"
       android:name="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       .../>


     <ImageView
       android:id="@+id/myLocationCustomButton"
       android:layout_width="@dimen/custom_my_location_button_size"
       android:layout_height="@dimen/custom_my_location_button_size"
       android:layout_gravity="bottom|end"
       android:background="@drawable/disc"
       android:backgroundTint="@android:color/white"
       android:padding="@dimen/margin_smallest"
       android:src="@drawable/ic_my_location"
       ../>
</FrameLayout>

然后,在活动中,我初始化并启动了一个google api客户端,我可以使用该按钮在点击过程中按钮时需要获取当前位置,请参阅下面的按钮点击监听器:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    ...
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    myLocationButton = rootView.findViewById(R.id.myLocationCustomButton);
}

@Override
protected void onStart() {
    googleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    myLocationButton.performClick();
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}

@Override
public void onMapReady(final GoogleMap googleMap) {
    this.googleMap = googleMap;

    myLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 16);
            MainActivity.this.googleMap.animateCamera(cameraUpdate, 250, null);
        }
    });

我的应用子模块的build.gradle也有:

ext {
    playServicesVersion = "8.4.0"
}

dependencies {
   ...     
   compile "com.google.android.gms:play-services-location:${playServicesVersion}"
   ...
}

希望有所帮助。

答案 9 :(得分:0)

我知道它已得到回答和接受,但这些答案对我没有用。也许在RelativeLayout.LayoutParams或甚至GoogleMaps中都有变化,因为它已被回答。

在我尝试使用现有答案的过程中,我意识到罗盘按钮可能有更多的布局参数规则,这些规则会覆盖我试图将指南针按钮移动到我想要的位置。我通过删除这样的几个参数进行了实验:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
                locationButton.getLayoutParams();

layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);

然后在几个答案中添加新规则。但它仍然无法按预期工作。通常情况下,按钮停留在左侧的某个位置。

我决定只创建新的LayoutParams并替换现有的,并且......它工作得很好!

我最初使用GoogleMapCompass的视图标记,但问题与GoogleMapMyLocationButton有关。所以我注释了GoogleMapCompass行并放入GoogleMapMyLocationButton行。

以下是MapFragment.java的代码:

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;


public class MapFragment extends Fragment implements OnMapReadyCallback {
    private static final String TAG = MapFragment.class.getSimpleName();

    private SupportMapFragment mapFragment = null;

    public MapFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        Log.d(TAG, "onCreateView()");

        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.setRetainInstance(true);

        mapFragment.getMapAsync(this);

        return view.findViewById(R.id.map);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d(TAG, "onMapReady()");

        View mapView = mapFragment.getView();

        moveCompassButton(mapView);
    }

    /**
     * Move the compass button to the right side, centered vertically.
     */
    public void moveCompassButton(View mapView) {
        try {
            assert mapView != null; // skip this if the mapView has not been set yet

            Log.d(TAG, "moveCompassButton()");

            // View view = mapView.findViewWithTag("GoogleMapCompass");
            View view = mapView.findViewWithTag("GoogleMapMyLocationButton");

            // move the compass button to the right side, centered
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            layoutParams.setMarginEnd(18);

            view.setLayoutParams(layoutParams);
        } catch (Exception ex) {
            Log.e(TAG, "moveCompassButton() - failed: " + ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }
}

答案 10 :(得分:0)

onCreate之后,以mapFragment.getMapAsync(this)方法添加此代码。它将我的位置按钮放在右下角。

View locationButton = ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
rlp.setMargins(0, 0, 30, 30);

答案 11 :(得分:0)

科特琳

var locationButton =mMapView.findViewWithTag("GoogleMapMyLocationButton")

Java

View locationButton =mMapView.findViewWithTag("GoogleMapMyLocationButton")

答案 12 :(得分:0)

就我而言,我想移动位置按钮,因为我正在以沉浸式/全屏模式查看 Google 地图,并将应用主题的 DisplayCutoutMode 设置为 shortEdges

    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

因此,Galaxy s10 的摄像头显示切口挡住了当前位置按钮的视图,因为显示屏中的摄像头位于屏幕的右上角。如果状态栏下有地图显示,您可能还想这样做。在这些情况下,我们需要定位按钮来适应SystemWindow。这是我在将 googleMap.isMyLocationEnabled 设置为 true..

后立即添加的一些 Kotlin 代码
  1. 获取对按钮的引用

     val locationButton: View? = fragmentContainerView.findViewWithTag("GoogleMapMyLocationButton")
    
  2. 应用您需要对按钮进行的​​任何更改。就我而言,我希望按钮适合系统窗口:

     locationButton?.fitsSystemWindows = true