使用谷歌地图和服务绘制多边形

你好,我试图在开车时画一条简单的线条。我想在后台收集位置数据,这就是为什么我使用服务。在服务端的代码如下所示:使用谷歌地图和服务绘制多边形

// 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) 

回答:

你不需要单独添加每个经纬度而是可以使用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);

以上是 使用谷歌地图和服务绘制多边形 的全部内容, 来源链接: utcz.com/qa/258418.html

回到顶部