Google Maps Api V2-移动时如何移动标记

时间:2016-12-16 17:05:31

标签: android google-maps nullpointerexception

美好的一天,我对google maps api v2有疑问,我想移动标记你位置的标记。但是我想每10秒钟更新一次标记的位置,你知道怎么做吗?

public class MapsActivity extends BaseMap {

private final static String LINE = "cfngCjzqtQXgBp@HlFao@oAh@yCdBit@wFpf@wc@~RaQlKgJtH}GjEcE~IuLpFmH`DwHrD_IxDqFbFmFjIsHnCaA`IgBjMmCdHkA`H_AvLoDvAK~B?dNfAvUrExHhB`KpBhFjA~GtAlFjAnJzBtJjBdFhAvGxAbPbDxOlDjPpDrF|@pHm@`IsD`OgIbGeDtKeGjM_HpKyFdGeDtGyFpBGnB~@xGtClHpBxD~@~Ds@|IoD`Uf@`]xGpG\\bKuDxPGxFlA`DtA~FlAtQvCfPdCzHj@rNq@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(新的LatLng(ula2,ulo2));

1 个答案:

答案 0 :(得分:-1)

您应该将location manager与方法requestLocationUpdates一起使用。每次位置变换器时,此方法都会触发事件onLocationChanged,然后您可以移动标记:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

    private GoogleMap mGoogleMap;
    private Location mLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        LocationManager locationManager = new LocationManager(this);
        locationManager.requestLocationUpdates (LocationManager.NETWORK_PROVIDER, 10, 10, this);

        mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        addMarker();

    }

    @Override
    public void onMapReady(GoogleMap map) {
        mGoogleMap = map;
       addMarker();

    }

    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        addMarker();
    }

    private void addMarker(){
        if ( mGoogleMap == null ){
           return;
        }
        mGoogleMap.clear();
        mGoogleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(mLlocation.getLatitude(), mLocation.getLongitude()));
    }

}