我是编程的新手,必须做一个项目,在该项目中我想在片段中实现一个google地图并读出位置并将其显示在地图上。我收到错误代码:
Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment' in row mapFragment = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map1);
通过初始化地图,但我还没有找到解决方案。
我认为这与androidX有关,但我不知道该写些什么。我也有一个问题,该问题是我在未授权的情况下打开应用程序时将其关闭。我希望有人知道这个问题。
片段:
package com.example.multitool;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class Location extends Fragment implements LocationListener, OnMapReadyCallback {
View view;
TextView textView_Longitude, textView_Latitute, textView_Altitute, textView_Speed;
SupportMapFragment mapFragment;
private LocationListener locationListener = null;
private LocationManager locationManager = null;
private final long MIN_TIME = 1000;
private final long MIN_DIST = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_location, container, false);
textView_Latitute = (TextView) view.findViewById(R.id.textView_Latitude);
textView_Longitude = (TextView) view.findViewById(R.id.textView_Longitude);
textView_Altitute = (TextView) view.findViewById(R.id.textView_Altitude);
textView_Speed = (TextView) view.findViewById(R.id.textView_Speed);
mapFragment = (SupportMapFragment)getFragmentManager().findFragmentById(R.id.map1);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
}
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, (LocationListener) this);
} else {
textView_Longitude.setText("no GPS Signal");
textView_Latitute.setText("no GPS Signal");
}
android.location.Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
return view;
}
@Override
public void onLocationChanged(android.location.Location location) {
double latitute = location.getLatitude();
double longitute = location.getLongitude();
double altitute = Math.round(location.getAltitude());
String string_latitute = String.valueOf(latitute);
String string_longitute = String.valueOf(longitute);
String string_altitute = String.valueOf(altitute);
textView_Latitute.setText("Latitute: " + string_latitute);
textView_Longitude.setText("Longitute: " + string_longitute);
textView_Altitute.setText("Altitute: " + string_altitute + "m");
float float_speed = location.getSpeed();
String string_speed = String.valueOf(float_speed);
textView_Speed.setText("Speed: " + string_speed + "m/s");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Location">
<!-- TODO: Update blank fragment layout -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map1"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/textView_Latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude"
android:textSize="20sp" />
<TextView
android:id="@+id/textView_Longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude"
android:textSize="20sp" />
<TextView
android:id="@+id/textView_Altitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Altitude"
android:textSize="20sp" />
<TextView
android:id="@+id/textView_Speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speed"
android:textSize="20sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</FrameLayout>