【问题标题】:Add "My Location" to Google Maps Fragment将“我的位置”添加到 Google 地图片段
【发布时间】:2017-04-05 10:29:23
【问题描述】:

我正在尝试在我的应用内的 Google 地图片段中添加“我的位置”按钮。到目前为止,这是我的代码:

package com.example.sander.app;

公共类 GoogleMaps 扩展 Fragment 实现 OnMapReadyCallback {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_gmaps, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    MapView mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);

    mapView.onResume(); // needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    mapView.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {

    LatLng marker = new LatLng(51.9244201, 4.4777325);

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12));

    googleMap.addMarker(new MarkerOptions().title("Testing").position(new LatLng(53.92, 4.47)));
    googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker));

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    googleMap.setMyLocationEnabled(true); // This is how I had implemented the setMyLocationEnabled method
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);

}

}

我已经将权限添加到我的 AndroidManifest.xml 和 googleMap.setMyLocationEnable(true);不起作用。

谁能帮我将按钮添加到我的 Google 地图片段中

【问题讨论】:

  • 地图中的位置按钮 googleMap.setMyLocationEnabled(true);应该工作
  • 您能发布一下您是如何实现 setMyLocationEnable(true) 的吗?您在 Manifest 中添加了哪些权限?
  • 我已将其添加到代码中
  • 只是为了确定,但您是否检查过 MapReady 上的方法是否会被调用?
  • @MichaelMeyer 我不知道。我确实知道我的地图显示在我的应用程序中并且放置标记有效!

标签: android google-maps android-fragments


【解决方案1】:

试试这个

mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);

【讨论】:

  • 那行不通,因为它不是一项活动。这是一个片段
  • @SanderBakker 它在我的片段中工作。它不需要 Activity 只需将此代码放入 onMapReady(GoogleMap googleMap)
  • 是的,我试过了,但之后由于 android SDK 21 和权限处理而出现调用错误
  • 你能发布你的代码吗?我需要看一个例子@SachinShelar
【解决方案2】:

如果有任何问题,请使用此代码并发表评论` 公共类 GoogleMaps 扩展 Fragment 实现 OnMapReadyCallback {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_trip, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    MapView mapView = (MapView) view.findViewById(R.id.fragment_map_trip);
    mapView.onCreate(savedInstanceState);

    mapView.onResume(); // needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    mapView.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {

    LatLng marker = new LatLng(51.9244201, 4.4777325);

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 12));

    googleMap.addMarker(new MarkerOptions().title("Testing").position(new LatLng(53.92, 4.47)));
    googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker));

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    googleMap.setMyLocationEnabled(true); // This is how I had implemented the setMyLocationEnabled method


}

}` 布局使用此代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_maps_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false">

<com.google.android.gms.maps.MapView
    android:id="@+id/fragment_map_trip"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</FrameLayout>

【讨论】:

  • 代码有效,但我看不到“我的位置”按钮
  • 您是在使用我的代码还是在代码中添加了我的代码?
  • 我正在使用您的代码。可以在本主题顶部看到(更新了代码)
  • 对于地图并启用我的位置,我们需要以下内容,因此请查看这些内容。 1. 清单文件中的权限。 2. build.gradle 文件中的 Gradle 依赖。 3. Google api 控制台设置(启用 api 并生成密钥)。 4. 来自服务器和 google-services.json 文件的 API 密钥。 5.我给你的​​Java和布局代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
相关资源
最近更新 更多