你好,我試圖在開車時畫一條簡單的線條。我想在後臺收集位置數據,這就是爲什麼我使用服務。在服務端的代碼如下所示:使用谷歌地圖和服務繪製多邊形
// 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: [email protected]
而且它還有我一個錯誤:
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 [email protected]
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)
stacktrace表示googleMap爲空。這可能發生的任何原因?檢查你是否正確地實例化了地圖。 –