gpt4 book ai didi

android - 使用 Google Maps API for Android 实时动态追踪路线

转载 作者:行者123 更新时间:2023-11-29 16:05:15 25 4
gpt4 key购买 nike

我是 Android 开发的新手,我正在尝试创建一个应用程序,它会在我走路时使用 map 叠加层追踪路线。我已经能够启动并运行 map ,并且可以让它显示我的运动,但是我不知道如何在 map 上显示所走的路线。我认为我需要使用折线来这样做,并且我尝试设置一个列表,然后从该列表中画出线,但我不确定这是否是合适的逻辑。任何帮助都将不胜感激。

这是我目前的代码:

ACTIVITY_MAIN_XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
/>

MAINACTIVITY.JAVA:

package com.example.google_maps_test;

import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource.OnLocationChangedListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import android.location.Location;
import android.location.LocationListener;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity implements GooglePlayServicesClient.ConnectionCallbacks,
OnConnectionFailedListener, LocationListener{

private OnLocationChangedListener mListener;
GoogleMap mMap;
LocationClient mLocationClient;
Location mCurrentLocation;
LocationRequest mLocationRequest;
List<LatLng>routePoints;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMap =((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
mLocationClient = new LocationClient(this,this, this);
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(1000);
mLocationRequest.setPriority(mLocationRequest.PRIORITY_HIGH_ACCURACY);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onConnected(Bundle dataBundle) {

Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
Location location = mLocationClient.getLastLocation();
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
routePoints.add(latLng);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
mMap.animateCamera(cameraUpdate);

}

@Override
public void onDisconnected() {

Toast.makeText(this, "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

public void onStart(){
super.onStart();
mLocationClient.connect();
}

public void onStop(){
mLocationClient.disconnect();
super.onStop();
}

public void activate(OnLocationChangedListener listener){
mListener = listener;
}

public void deactivate(){
mListener = null;
}

@Override
public void onLocationChanged(Location location) {

if(mListener != null){
mListener.onLocationChanged(location);
}

LatLng mapPoint = new LatLng(location.getLatitude(), location.getLongitude());
routePoints.add(mapPoint);
Polyline route = mMap.addPolyline(new PolylineOptions());
route.setPoints(routePoints);


}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}
}

ANDROIDMANIFEST_XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.google_maps_test"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.google_maps_test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCOqSOYCyR-0bji10qcHa1WByfGoW-2ZsU"/>
</application>

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

</manifest>

当我运行这个应用程序时,它因 OnLocationChanged 方法中的空指针异常而崩溃。

在此先感谢您的帮助!

最佳答案

对于任何想知道错误的人,我发现 routePoints 从未被初始化。

关于android - 使用 Google Maps API for Android 实时动态追踪路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com