Jak*_*kob 83 java android google-maps-android-api-2 android-maps-v2
我正在使用谷歌地图Android API v2,我需要一种方法来获取"我的位置"按钮的位置.
我得到这样的"我的位置"按钮:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// This gets the button
map.setMyLocationEnabled(true);
Run Code Online (Sandbox Code Playgroud)
fab*_*uis 86
您可以获取"我的位置"按钮并将其移动,如:
public class MapFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
// Get the button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2);
// and next place it, for exemple, on bottom right (as Google Maps app)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Asc*_*ris 69
只需使用GoogleMap.setPadding(左,上,右,下),它允许您指示可能被其他视图遮挡的地图部分.设置填充会重新定位标准地图控件,相机更新将使用填充区域.
https://developers.google.com/maps/documentation/android/map#map_padding
Rya*_*yan 15
这可能不是最佳解决方案,但您可以将自己的按钮放在地图上并自行处理.它需要以下内容: -
1)将地图放在frameLayout中,然后在顶部添加按钮.例如
<FrameLayout
android:id="@+id/mapFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.MapFragment"
map:mapType="normal"
map:uiCompass="true" />
<ImageButton
android:id="@+id/myMapLocationButton"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="bottom|right"
android:background="@drawable/myMapLocationDrawable"
android:contentDescription="My Location" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
2)编辑地图UI设置,以便在调用setMyLocationEnabled(true)时不显示该按钮.您可以通过map.getUiSettings()完成此操作.setMyLocationButtonEnabled(假);
3)处理新按钮的单击以模拟提供的按钮所执行的操作.例如,调用mMap.setMyLocationEnabled(...); 并将地图平移到当前位置.
希望有所帮助,或希望有人为您提供更简单的解决方案;-)
Sah*_*ain 14
它已经在上面解释过.只是fabLouis答案的一小部分内容.您也可以从SupportMapFragment获取地图视图.
/**
* Move the button
*/
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
findFragmentById(R.id.map);
View mapView = mapFragment.getView();
if (mapView != null &&
mapView.findViewById(1) != null) {
// Get the button view
View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(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);
}
Run Code Online (Sandbox Code Playgroud)
Cor*_*ehn 12
我不想看到其他人正在使用的这些神奇的视图ID,我建议使用标签来找到MapView孩子.
这是我将" 我的位置"按钮放在" 缩放"控件上方的解决方案.
// Get map views
View location_button =_mapView.findViewWithTag("GoogleMapMyLocationButton");
View zoom_in_button = _mapView.findViewWithTag("GoogleMapZoomInButton");
View zoom_layout = (View) zoom_in_button.getParent();
// adjust location button layout params above the zoom layout
RelativeLayout.LayoutParams location_layout = (RelativeLayout.LayoutParams) location_button.getLayoutParams();
location_layout.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
location_layout.addRule(RelativeLayout.ABOVE, zoom_layout.getId());
Run Code Online (Sandbox Code Playgroud)
小智 11
我通过使用下面的代码将我的位置按钮重新定位到视图的右下角来解决我的地图片段中的这个问题,这是我的Maps Activity.java: -
在onCreate()方法中添加这行代码,
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapView = mapFragment.getView();
mapFragment.getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)
这里是onMapReady()代码: -
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望,这将解决你的问题.谢谢.
请参阅以下方法.它位于一个扩展SupportMapFragment的类中.它获取按钮的容器视图,并在底部显示,水平居中.
/**
* Move my position button at the bottom of map
*/
private void resetMyPositionButton()
{
//deep paths for map controls
ViewGroup v1 = (ViewGroup)this.getView();
ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
ViewGroup v4 = (ViewGroup)v3.getChildAt(1);
//my position button
View position = (View)v4.getChildAt(0);
int positionWidth = position.getLayoutParams().width;
int positionHeight = position.getLayoutParams().height;
//lay out position button
RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight);
int margin = positionWidth/5;
positionParams.setMargins(0, 0, 0, margin);
positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
position.setLayoutParams(positionParams);
}
Run Code Online (Sandbox Code Playgroud)
首先,获取谷歌地图视图:
View mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getView();
Run Code Online (Sandbox Code Playgroud)
然后找到MyLocation按钮(来自Android Studio调试器的id):
View btnMyLocation = ((View) mapView.findViewById(1).getParent()).findViewById(2);
Run Code Online (Sandbox Code Playgroud)
最后,只需为MyLocation按钮设置新的RelativeLayout参数(在这种情况下,将父级右对齐+居中对齐):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dp
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.setMargins(0, 0, 20, 0);
btnMyLocation.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
繁荣!现在你可以随意移动它;)
如果您只想启用位置指示(蓝点)但不需要默认的"我的位置"按钮:
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这样你也可以在你想要的地方画出你自己的按钮而不会有这样的奇怪的东西mapView.findViewById(1).getParent()).
| 归档时间: |
|
| 查看次数: |
71893 次 |
| 最近记录: |