0

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
tom
  • 3
  • 2
  • You're not assigning the `locationService` reference, so it will always be null. In general though, the approach you're taking here is not good. You shouldn't have a direct reference to a Service. Also, you're trying to poll for values, when you should instead be waiting for a callback from the Service once it has a location. You can use a bound service, or use a BroadcastReceiver, see here: https://stackoverflow.com/questions/10179470/how-to-get-location-from-service – Daniel Nugent Oct 03 '19 at 17:03
  • but I AM getting location data from the service, because I have this ` 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); ` showing me current GPS coordinates accurately, so how come I cannot use those coordinates on the map? I am already using a bound service in this code. I just didn't post the full code. – tom Oct 03 '19 at 17:16

1 Answers1

0

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);
        }
    });
}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • This seems to work in that it does actually place the marker at the location the virtual device sets it to, but if I then go into the extended controls in the AVM and set a new location, it immediately puts it back at the first coordinates? shouldn't I keep polling for new updates if I want to keep the marker moving? meaning I should put the handler.postdelayed In the IF too? Because I want to keep tracking the current user location. – tom Oct 03 '19 at 17:47
  • Sure, I didn't realize you wanted to keep polling. Just updated the answer. – Daniel Nugent Oct 03 '19 at 17:51
  • Your solution is working great, the only thing I'd like to be able to do is basically 'refresh' the icon in the new location. the current solution adds another marker while keeping the original intact. how would I be able to do this? – tom Oct 03 '19 at 17:59
  • Just keep a member variable with a reference to the current location Marker. Take a look at the answer here, and how keeps a member variable `mCurrLocationMarker`: https://stackoverflow.com/a/34582595/4409409 – Daniel Nugent Oct 03 '19 at 19:48