-1
美好的一天,我有一個谷歌地圖api v2的問題我想移動標記您的位置的標記。但我想每10秒更新一次標記的位置,你有什麼想法如何做到這一點?Google Maps Api V2-如何在移動時移動標記
public class MapsActivity extends BaseMap {
private final static String LINE = "[email protected]@[email protected]@[email protected]@~RaQlKgJtH}GjEcE~IuLpFmH`DwHrD_IxDqFbFmFjIsHnCaA`IgBjMmCdHkA`H_AvLoDvAK~B?dNfAvUrExHhB`KpBhFjA~GtAlFjAnJzBtJjBdFhAvGxAbPbDxOlDjPpDrF|@[email protected]`IsD`[email protected][email protected][email protected]|IoD`[email protected]`]xGpG\\bKuDxPGxFlA`[email protected]@tKeCtIkBbJoCxGgCtI}DnGkChS_H`OuExKaEtUaHfRsFdIyBpF_BhHoBzJ}C|N{@hCI`EeBpF{NpAqD|CuIrEnBxFfCaAlC";
double ula, ulo, ula2 ,ulo2;
final Context context1 = this;
private MarkerOptions a = new MarkerOptions();
private Marker m;
@Override
protected void startDemo() {
List<LatLng> decodedPath = PolyUtil.decode(LINE);
getMap().addPolyline(new PolylineOptions().addAll(decodedPath).color(Color.RED));
LocationManager lManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context1, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context1, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location lastKnownLocation = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
ula= lastKnownLocation.getLatitude();
ulo=lastKnownLocation.getLongitude();
Log.v("Lactitud", String.valueOf(ula));
Log.v("Longitud", String.valueOf(ulo));
MarkerOptions a = new MarkerOptions().position(new LatLng(ula,ulo));
a.title("Tú");
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.293139, -97.88466440000002), 12));
getMap().addMarker(new MarkerOptions().position(new LatLng(22.360463,-97.908935)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).setTitle("Urban Azteca");
getMap().addMarker(new MarkerOptions().position(new LatLng(22.21510,-97.85956)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).setTitle("Urban Tampico");
getMap().addMarker(new MarkerOptions().position(new LatLng(ula,ulo))).setTitle("Tú");
int delay = 1000;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
LocationManager lManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context1, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context1, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location lastKnownLocation = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
ula= lastKnownLocation.getLatitude();
ulo=lastKnownLocation.getLongitude();
m.setPosition(new LatLng(ula2, ulo2));
}}, 10000);
}
}
而當我運行它會給出這個錯誤。
12-16 11:02:05.984 26736-26736/com.example.maygea.pruebamaps E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.maygea.pruebamaps, PID: 26736
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.model.Marker.setPosition(com.google.android.gms.maps.model.LatLng)' on a null object reference
at com.example.maygea.pruebamaps.MapsActivity$1.run(MapsActivity.java:85)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
我認爲它是我的m.setPosition(new LatLng(ula2,ulo2));
m爲空,因此空指針異常 – Shmuel