Search code examples
androidgoogle-mapslocationhandler

how to update map icon via Handler


I am a very novice programmer. I am using a handler to get my location data from a service to my main activity but I would like to update the map using the code from the service.

The GPS coordinates are correctly sent via the handler, but I seem to be stuck on how to use the handler to actually present the data by updating the map/ icons on the map.

The map keeps putting the marker at 0.0, nor does it position the map at the new location sent via the handler.

   //get GPS latitude and Longitude from service here
    private void displayLatLong() {
        final TextView latitudeView = (TextView) findViewById(R.id.latitude);
        final TextView longitudeView = (TextView) findViewById(R.id.longitude);
        final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
        final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);

        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                double latitude = 0.0;
                double longitude = 0.0;

                //latitudeCoords = "not empty"; //debug
                //longitudeCoords = "not empty"; //debug

                if(bound && locationService != null) {
                    latitude = locationService.getLastLatitude();
                    longitude = locationService.getlastLongitude();
                }
                String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
                String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
                latitudeView.setText(latitudeStr);
                longitudeView.setText(longitutdeStr);

                latCoords = latitude;
                longCoords = longitude;

               // longitudeCoordsView.setText(longitudeCoords); //debug
               // latitudeCoordsView.setText(longitudeCoords); //debug

                handler.postDelayed(this, 1000);
            }
        });
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the SupportMapFragment and request notification
        // when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);


        //Bind location service here so it keeps running even if activity is stopped.
        Intent intent = new Intent(this, LocationService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);

        displayDistance();
        displayLatLong();
    }

@Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        LatLng player= new LatLng(latCoords, longCoords);
        mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
        mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
    }locat

Solution

  • You just need to update the map once you get the location.

    Add a method that will update the map:

    private void updateMapLocation() {
      LatLng player= new LatLng(latCoords, longCoords);
      mMap.addMarker(new MarkerOptions().position(player).title("current player position"));//set marker with player position and description
      mMap.moveCamera(CameraUpdateFactory.newLatLng(player)); //move Camera to player position
    }
    

    Then call this method once you have the latCoords and longCoords member variables populated:

    //get GPS latitude and Longitude from service here
    private void displayLatLong() {
        final TextView latitudeView = (TextView) findViewById(R.id.latitude);
        final TextView longitudeView = (TextView) findViewById(R.id.longitude);
        final TextView latitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
        final TextView longitudeCoordsView = (TextView) findViewById(R.id.latitudecoordsView);
    
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                double latitude = 0.0;
                double longitude = 0.0;
    
                if(bound && locationService != null) {
                    latitude = locationService.getLastLatitude();
                    longitude = locationService.getlastLongitude();
                }
                String latitudeStr = String.format(Locale.getDefault(), "%1$, .5f lat", latitude);
                String longitutdeStr = String.format(Locale.getDefault(), "%1$, .5f long", longitude);
                latitudeView.setText(latitudeStr);
                longitudeView.setText(longitutdeStr);
    
    
                latCoords = latitude;
                longCoords = longitude;
    
                // Add call to update map with location:
                if (latCoords > 0 && longCoords > 0) {
                  updateMapLocation()
                }
    
                handler.postDelayed(this, 1000);
            }
        });
    }