我是不熟悉Android编程的人,需要制作GPS应用程序,我创建了一个带有图形界面的小型应用程序进行测试,但我不进行地图操作,此操作返回null,因此我无法解决。我把我的代码作为一个教程,谢谢
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="co.quindio.sena.ejemplomaparutas">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"></activity>
</application>
</manifest>
公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {
private GoogleMap mMap;
Double latInicial,longInicial,latFinal,longFinal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* 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) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
/////////////
LatLng center = null;
ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
// setUpMapIfNeeded();
// recorriendo todas las rutas
for(int i=0;i<Utilidades.routes.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Obteniendo el detalle de la ruta
List<HashMap<String, String>> path = Utilidades.routes.get(i);
// Obteniendo todos los puntos y/o coordenadas de la ruta
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
if (center == null) {
//Obtengo la 1ra coordenada para centrar el mapa en la misma.
center = new LatLng(lat, lng);
}
points.add(position);
}
// Agregamos todos los puntos en la ruta al objeto LineOptions
lineOptions.addAll(points);
//Definimos el grosor de las Polilíneas
lineOptions.width(2);
//Definimos el color de la Polilíneas
lineOptions.color(Color.BLUE);
}
// Dibujamos las Polilineas en el Google Map para cada ruta
mMap.addPolyline(lineOptions);
LatLng origen = new LatLng(Utilidades.coordenadas.getLatitudInicial(), Utilidades.coordenadas.getLongitudInicial());
mMap.addMarker(new MarkerOptions().position(origen).title("Lat: "+Utilidades.coordenadas.getLatitudInicial()+" - Long: "+Utilidades.coordenadas.getLongitudInicial()));
LatLng destino = new LatLng(Utilidades.coordenadas.getLatitudFinal(), Utilidades.coordenadas.getLongitudFinal());
mMap.addMarker(new MarkerOptions().position(destino).title("Lat: "+Utilidades.coordenadas.getLatitudFinal()+" - Long: "+Utilidades.coordenadas.getLongitudFinal()));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(center, 15));
/////////////
}
}
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="co.quindio.sena.ejemplomaparutas.MapsActivity" />
https://youtu.be/lDpOkpIPlts这是给你的。您可以检查此链接