简体   繁体   中英

How to add custom Marker in Google maps besides onMapReady

I'm struggling with one question. How can I implement method in Google Maps Activity to add marker.

I wanna to create method with two params (lat, lon) and in this place adding markers every time when my restfull client give me new data.

My problem is in fact that I wanna stay with MVC convention. When I'm including retrofit controller into MapsActivity all works fine.

Problem begins when I send data from Controller to MapsActivity (Ends with NullPointerException).

MapsActivty:

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;

private Handler mHandler = new Handler();



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);


}

public void onCarChangeLocation(Double lat, Double lon) {


    LatLng carLatLng = new LatLng(lat, lon);
    mMap.addMarker(new MarkerOptions().position(carLatLng).title("Integra Location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(carLatLng, 15.5f));


}




public Runnable getDataRunnable = new Runnable() {
    @Override
    public void run() {
        Controller controller = new Controller();
        controller.getData();
        mHandler.postDelayed(this,1000);
    }
};

@Override
public void onMapReady(GoogleMap googleMap) {

     mMap = googleMap;
     getDataRunnable.run();


}



}

Controller:

public class Controller {

public static final String BASE_URL = "server_ip";
private static Retrofit retrofit = null;
 public Double lat;
public Double lon;
public void getData() {

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

    }

    JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
    Call<OutputModel> call = jsonPlaceHolderApi.getCords();

    call.enqueue(new Callback<OutputModel>() {
        @Override
        public void onResponse(Call<OutputModel> call, retrofit2.Response<OutputModel> response) {

            if (!response.isSuccessful()) {
                // onFailTV.setText("Code: " + response.code());
                return;
            }


           // mMap.clear();
            lat = response.body().getLatitude();
            lon = response.body().getLongitude();
            MapsActivity mapsActivity = new MapsActivity();
            mapsActivity.onCarChangeLocation(lat, lon);




        }


        @Override
        public void onFailure(Call<OutputModel> call, Throwable t) {

        }
    });
}
}

Thanks for any reply.

Update your MapsActivity code as bellow

public class MapsActivity extends FragmentActivity implements
        OnMapReadyCallback {
    private GoogleMap mMap;
    private Handler mHandler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        getDataRunnable.run();
    }
    LatLng carLatLng;
    public void onCarChangeLocation(Double lat, Double lon) {
        if (mMap != null) {//Check here map is not initialized 
            carLatLng = new LatLng(lat, lon);
            mMap.addMarker(new MarkerOptions().position(carLatLng).title("Integra Location"));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(carLatLng, 15.5f));
        } else {
            carLatLng = new LatLng(lat, lon);
        }

    }
    public Runnable getDataRunnable = new Runnable() {
        @Override
        public void run() {
            Controller controller = new Controller();
            controller.getData();
            mHandler.postDelayed(this, 1000);
        }
    };
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (carLatLng != null) { //recall for setup pin
            onCarChangeLocation(carLatLng.latitude, carLatLng.longitude);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM