Android 加上「;“我的位置”;谷歌地图片段
我正在尝试将“我的位置”按钮添加到我的应用程序中的谷歌地图片段中。这是我目前的代码:Android 加上「;“我的位置”;谷歌地图片段,android,google-maps,android-fragments,Android,Google Maps,Android Fragments,我正在尝试将“我的位置”按钮添加到我的应用程序中的谷歌地图片段中。这是我目前的代码: package com.example.sander.app; 公共类GoogleMaps在MapReadyCallback上扩展了片段实现{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inf
package com.example.sander.app;
公共类GoogleMaps在MapReadyCallback上扩展了片段实现{
@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);
}
@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
}
}
我已经向AndroidManifest.xml和googleMap.setMyLocationEnable(true)添加了权限;不起作用
有人能帮我把这个按钮添加到我的谷歌地图片段吗
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
请使用此代码并在出现任何问题时进行注释` 公共类GoogleMaps在MapReadyCallback上扩展了片段实现{
@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);
}
@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>
用于地图谷歌地图中的位置按钮。setMyLocationEnabled(true);您是否可以发布您是如何实现setMyLocationEnable(true)的?您在清单中添加了什么样的权限?我将其添加到代码中只是为了确定是否会调用MapReady上的方法?@MichaelMeyer我不知道。我知道我的地图显示在我的应用程序中&放置标记很有效!这不起作用,因为它不是一项活动。这是一个fragment@SanderBakker它在我的片段中起作用。它不需要活动,只需将此代码放入onMapReady(GoogleMap GoogleMap)
是的,我试过了,但在那之后,由于android SDK 21和权限处理,我收到了调用错误。你能发布你的代码吗?我需要看一个例子@sachinshellarcode工作,但我看不到“我的位置”按钮您使用的正是我的代码,或者您在代码中添加了我的代码?我使用的是您的代码。您可以在本主题的顶部看到它(更新了代码),用于地图和启用我的位置。我们需要以下内容,请查看这些内容。1.清单文件中的权限。2.build.Gradle文件中的Gradle依赖项。3.Google api控制台设置(启用api并生成密钥)。4.服务器和google-services.json文件中的Api密钥。5.我给你的Java和布局代码。