我的应用程序在片段上显示空白谷歌地图。我只是从MainActivity中调用该片段。我在谷歌搜索/阅读,但没有人帮助我。我没有收到任何错误,我使用了适当的API密钥。 以下是我的课程/ mxls:
public class MainActivity extends AppCompatActivity {
private Button map_me;
private Button bCordinates;
public static Location mLastKnownLocation;
private FusedLocationProviderClient mFusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (true) {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
//Get the Location of the current device
mLastKnownLocation = task.getResult();
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
bCordinates = (Button)findViewById(R.id.bcordinates);
bCordinates.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Toast.makeText(getApplicationContext(),""+mLastKnownLocation.getLatitude()+" , " +
" "+mLastKnownLocation.getLongitude(),Toast.LENGTH_LONG).show();
}
});
map_me = (Button)findViewById(R.id.bmap);
map_me.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyMapFragment myMapFragment = new MyMapFragment();
fragmentTransaction.add(R.id.fragment_container, myMapFragment);
fragmentTransaction.commit();
}
});
}
}
public class MyMapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap googleMap;
private MapView mapView;
private View view;
private FusedLocationProviderClient mFusedLocationProviderClient;
public MyMapFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.map_layout, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mapView = (MapView) getView().findViewById(R.id.map);
if(mapView!=null)
{
mapView.onCreate(null);
mapView.onResume();
mapView.getMapAsync(this);
}
}
@Override
public void onMapReady(final GoogleMap mGoogleMap) {
{
googleMap = mGoogleMap;
LatLng mLatLng = new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude());
googleMap.addMarker(new MarkerOptions().position(mLatLng).title("You're here"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng,15));
mapView.onResume();
}
}
}
地图片段布局
<?xml version="1.0" encoding="utf-8"?>
<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=".MyMapFragment">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
/>
</FrameLayout>
主要活动布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bdushimi.android2.MainActivity"
>
<Button
android:id="@+id/bmap"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:text="MAP Me!"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.067"
tools:layout_editor_absoluteY="32dp" />
<Button
android:id="@+id/btext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT ME!"
tools:layout_editor_absoluteY="32dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.464" />
<Button
android:id="@+id/bcordinates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cordinates"
android:layout_marginEnd="8dp"
tools:layout_editor_absoluteY="32dp"
android:layout_marginRight="28dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintHorizontal_bias="0.949"
app:layout_constraintLeft_toLeftOf="parent" />
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="333dp"
android:layout_height="370dp"
android:orientation="vertical"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="125dp"
app:layout_constraintHorizontal_bias="0.514"></LinearLayout>
</android.support.constraint.ConstraintLayout>
请帮助我找出我做错了什么。
答案 0 :(得分:0)
我认为您应该根据您的可用性在地图布局xml文件中使用MapFragment或SupportMapFragment。
目前: 替换:
with:
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/headerLayout"
tools:context="com.dxs.rnf.winwin.main.ui.fragments.DashboardMapFragment"
android:name="com.google.android.gms.maps.MapFragment"/>