你好我试图在开车时画一条简单的线。我想在后台收集位置数据,这就是我使用服务的原因。服务端的代码如下所示:
// THIS IS FOR GPS
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider){
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
@Override
public void onLocationChanged(Location location) {
Intent mapIntent = new Intent();
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
mapIntent.putExtra("Latitude", latitude);
mapIntent.putExtra("Longitude", longitude);
mapIntent.setAction(MAP_ACTION);
sendBroadcast(mapIntent);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e(TAG, "onStatusChanged: " + provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, "onProviderEnabled: " + provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, "onProviderDisabled: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
我使用putExtra
发送纬度和经度,我在FragmentActivity中读取此内容。
class MapBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
LatLng temp = new LatLng(extras.getDouble("Latitude"),extras.getDouble("Longitude"));
points.add(temp);
try {
googleMap.clear();
PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
for (int i = 0; i < points.size(); i++) {
LatLng point = points.get(i);
options.add(point);
Log.d("LATLONG", String.valueOf(options));
}
if (options != null)
{
line = googleMap.addPolyline(options);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我能够在片段一侧读取纬度和经度,所以看起来一切正常但它不会划线。 但是期权价值如下:
D / LATLANG:com.google.android.gms.maps.model.PolylineOptions@21ea8168
它给我一个错误:
W / System.err:java.lang.NullPointerException
ArrayList<LatLng> points;
初始化onCreateView
我在这几个小时内挣扎,我无法找到解决方案。 我会非常感谢你的帮助。
错误消息是:
12-02 23:19:35.079 5423-5423/com.example.mattkopacz.obdmenu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mattkopacz.obdmenu, PID: 5423
java.lang.RuntimeException: Error receiving broadcast Intent { act=MAP_ACTION flg=0x10 (has extras) } in com.example.mattkopacz.obdmenu.Menu1$MapBroadcast@21f0fb18
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mattkopacz.obdmenu.Menu1$MapBroadcast.onReceive(Menu1.java:260)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您无需单独添加每个LatLng,而是可以使用polyOptions.addAll(points)
将整个列表添加到一起。
final PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(Color.BLUE);
polyOptions.width(5);
polyOptions.geodesic(true);
polyOptions.jointType(ROUND);
polyOptions.addAll(points);
googleMap.addPolyline(polyOptions);