Java 标记未显示在地图片段上
这是我第一次使用android studio,我创建了一个导航栏,其中我使用了一个包含3个片段的主机片段,其中一个是地图片段,地图正在显示,但我无法获得标记,我不知道我是否弄乱了地图片段,但任何帮助或提示都将不胜感激。 多谢各位 MapFragment.javaJava 标记未显示在地图片段上,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,这是我第一次使用android studio,我创建了一个导航栏,其中我使用了一个包含3个片段的主机片段,其中一个是地图片段,地图正在显示,但我无法获得标记,我不知道我是否弄乱了地图片段,但任何帮助或提示都将不胜感激。 多谢各位 MapFragment.java package com.example.sanad; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; im
package com.example.sanad;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
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 MapsFragment extends Fragment {
private OnMapReadyCallback callback = new OnMapReadyCallback() {
/**
* 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) {
LatLng sydney = new LatLng(-50, 50);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_maps, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment mapFragment =
(SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(callback);
}
}
}
片段映射.xml
<?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:id="@+id/map_view"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsFragment"
tools:ignore="DuplicateIds">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:context=".MapsActivity">
</fragment>
</RelativeLayout>
代码似乎没问题,您可以尝试设置一些调试点,直到调用代码的哪一部分,尽管我确实注意到一件事,您可以将
moveCamera
方法调用更新为animateCamera
,因为它可以为移动设置动画,您可能知道它是否工作。我还更新了正确的坐标
LatLng悉尼=新LatLng(-33.852151.211);
addMarker(新的MarkerOptions()
.职位(悉尼)
.标题(“悉尼的标志”);
googleMap.animateCamera(CameraUpdateFactory.newLatLng(悉尼));
您确定Latlang是正确的吗?它不是sydney的坐标,但我不知道该位置,并且相机没有移动,因此不会调用onMapReady上的孔。请尝试将onViewCreated内容移动到onCreateViewsame问题:(不幸的是,标记和动画相机不工作:(.我得到了地图,但是这2个没有。无论如何谢谢。@SyyKee你能调试一下代码被调用的点吗?也许整个mapReady
回调都不起作用了?