arrayadapter displaying wrong data + cant route in google map

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 434 Views - Last Post: 13 March 2019 - 09:09 AM

#16 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: arrayadapter displaying wrong data + cant route in google map

Posted 11 March 2019 - 02:47 PM

yes i can zoom out still and see the areas those points are at. those location points are no more than a 5 minute walk from me.

i added width and color to it(i should have done that to begin with) and still nothing.

I also noticed that polyline1 is greyed out because its never used. Am i missing something?

Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .width(5)
                .color(Color.RED)
                .add(new LatLng(42.3149, 83.0364),
                        new LatLng(42.3183, 83.0373)));


Was This Post Helpful? 0
  • +
  • -

#17 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 11 March 2019 - 03:42 PM

Quote

Call GoogleMap.addPolyline() to add the polyline to the map.
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
        .clickable(true)
        .add(
                new LatLng(-35.016, 143.321),
                new LatLng(-34.747, 145.592),
                new LatLng(-34.364, 147.891),
                new LatLng(-33.501, 150.217),
                new LatLng(-32.306, 149.248),
                new LatLng(-32.491, 147.309)));



https://developers.g...olygon-tutorial

The IDE is probably thinking you never used polyline1, but as far as I can tell all you have to do is instantiate the Polyline using .addPolyLine method of GoogleMap. Then all you would use that variable for is re-assigning it's properties. I'll keep looking into it but no idea at this point. Let me know if you figure it out.
Was This Post Helpful? 0
  • +
  • -

#18 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: arrayadapter displaying wrong data + cant route in google map

Posted 11 March 2019 - 04:22 PM

will do. It should be standard because im looking at the polyline demo and i cant see what im missing.
Was This Post Helpful? 0
  • +
  • -

#19 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 11 March 2019 - 07:27 PM

This is a pretty useful topic; I'm very interested in the resolution. Normally, I would create a project and then test it myself; however, I use Xamarin for Android projects, and I already know the syntax conversion from java would be a nightmare. I don't have that kind of free time on my hands. One thing you might want to do next is create a barebones app with only the Google map and a polyline. See if you can get it working in a super basic context. I'll look into it further tomorrow; I barely slept last night so exhausted atm. Keep me posted
Was This Post Helpful? 0
  • +
  • -

#20 Bobby_Bubbles   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 291
  • Joined: 13-March 18

Re: arrayadapter displaying wrong data + cant route in google map

Posted 12 March 2019 - 01:36 PM

So i stripped it bare for the demo i had and this is the code.

public class Maps extends AppCompatActivity implements OnMapReadyCallback {
    private static final LatLng ADELAIDE = new LatLng(42.3149, -83.0364);
    private static final LatLng DARWIN = new LatLng(42.3183, -83.0373);
    private static final LatLng MELBOURNE = new LatLng(42.3201, -83.0335);
    private Polyline mMutablePolyline;
    @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);
    }
    @Override
    public void onMapReady(GoogleMap map) {
        mMutablePolyline = map.addPolyline(new PolylineOptions()
                .color(Color.BLUE)
                .width(10)
                .add(ADELAIDE, DARWIN, MELBOURNE));
        mMutablePolyline.setStartCap(new RoundCap());
        mMutablePolyline.setEndCap(new RoundCap());
        mMutablePolyline.setPattern(null);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(ADELAIDE, 15));
    }
}



That above is the basic code to draw polylines. It works.

Now this is my code.

public class Index extends AppCompatActivity implements OnMapReadyCallback {
    public static String[] account;

    private static final LatLng ADELAIDE = new LatLng(42.3149, -83.0364);
    private static final LatLng DARWIN = new LatLng(42.3183, -83.0373);
    private static final LatLng MELBOURNE = new LatLng(42.3201, -83.0335);
    private Polyline mMutablePolyline;

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    private boolean mPermissionDenied = false;
    private GoogleMap mMap;
    LocationManager locationManager;
    LocationListener locationListener;
    String[][] mobileArray = {
            new String[] {"150 Goyeau St, Windsor, ON N9A 6J5 [3:30]","1g Bubba Kush","Meet at back","3:30pm"},
            new String[] {"2000 Talbot Road West, Windsor, ON N9A 6S4 [4:00]","1oz Rockstar","Watch for dog","4:00pm"},
            new String[] {"350 City Hall Square W, Windsor, ON N9A 6S1 [4:30]","2 pens","Go in the back","4:30pm"}
    };

    public void centreMapOnLocation(Location location, String title){
        LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }
    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }
    public LatLng getLocationFromAddress(Context context,String strAddress) {
        Geocoder coder = new Geocoder(context);
        List<Address> address;
        LatLng p1 = null;
        try {
            address = coder.getFromLocationName(strAddress, 5);
            if (address == null) {
                return null;
            }
            Address location = address.get(0);
            p1 = new LatLng(location.getLatitude(), location.getLongitude() );
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        Toast.makeText(context,p1.toString(),Toast.LENGTH_SHORT).show();
        return p1;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tVDriver = (TextView)findViewById(R.id.tVDriver);
        tVDriver.setText(account[0]);
        final TextView tVSpecial = (TextView)findViewById(R.id.tVSpecial);
        final TextView tVProduct = (TextView)findViewById(R.id.tVProduct);
        String[] addressArray = new String[mobileArray.length];
        for(int i=0; i<mobileArray.length; i++) {
            addressArray[i] = mobileArray[i][0];
        }
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, addressArray);
        ListView listView = (ListView) findViewById(R.id.lVDelivery);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?>adapter, View v, int position, long id){
                getLocationFromAddress(Index.this,mobileArray[position][0]);
                tVSpecial.setText(mobileArray[position][2]);
                tVProduct.setText(mobileArray[position][1]);
            }
        });
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        //BackWork bwork = new BackWork(this);
        //bwork.execute("directions",null,null);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMutablePolyline = googleMap.addPolyline(new PolylineOptions()
                .color(Color.BLUE)
                .width(10)
                .add(ADELAIDE, DARWIN, MELBOURNE));
        mMutablePolyline.setStartCap(new RoundCap());
        mMutablePolyline.setEndCap(new RoundCap());
        mMutablePolyline.setPattern(null);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ADELAIDE, 15));
        locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationchanged(Location location) {
                //centreMapOnLocation(location,"Your Location");
                //Toast.makeText(Index.this,location.toString(),Toast.LENGTH_SHORT).show();
                TextView tVCoords = (TextView)findViewById(R.id.tVCoords);
                tVCoords.setText("X: "+location.getLatitude()+" Y: "+location.getLongitude());
                LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(userLocation).title("Here"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centreMapOnLocation(lastKnownLocation,"Your Location");
        } else {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        }
    }
    private void enableMyLocation() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);
        } else if (mMap != null) {
            mMap.setMyLocationEnabled(true);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }
        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            enableMyLocation();
        } else {
            mPermissionDenied = true;
        }
    }
    @Override
    protected void onresumeFragments() {
        super.onresumeFragments();
        if (mPermissionDenied) {
            showMissingPermissionerror();
            mPermissionDenied = false;
        }
    }
    private void showMissingPermissionerror() {
        PermissionUtils.PermissionDeniedDialog
                .newInstance(true).show(getSupportFragmentManager(), "dialog");
    }
}



This is the part that broke the polylines

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centreMapOnLocation(lastKnownLocation,"Your Location");
        } else {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        }



Whenever i disable this it draws the coordinates fine. I didnt plot the routes yet as i still have to work out my current location.

Now the problem is this. If i comment this out it breaks my current location. If i enable this my current location can be found and the text field gets updated with my present coordinates but tyhe lines i drew is gone.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#21 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 12 March 2019 - 03:38 PM

Hey, I've been extremely busy today so didn't have a chance to respond.


Can you try instantiating

Location lastKnownLocation;

at the start of the class?

I don't think it should matter, since it's fed into a method, but I would try it anyway. Also, might want to remove the conditional for testing purposes. Try breaking that up with log commands so we can see which part is failing. Keep in mind that when we're debugging we want to remove as much extra code as possible. In fact, you may have to make a barebones app with only those two functions (and anything required for the app) so that we can figure this out. At the bare least, use only exactly what we need and you might want to create buttons for each part of that conditional so we can do them one at a time and check the values being fed into other methods. Make a copy of the project for testing purposes and start splitting that up.

Certainly is extremely odd, I really don't have a lot of other ideas, but I'll let this simmer in my brain and come back in a bit here.

EDIT: actually you can also use breakpoints to check the values, but I would still try removing the conditional and instantiating Location at the beginning of your class

This post has been edited by hexagod: 12 March 2019 - 03:43 PM

Was This Post Helpful? 0
  • +
  • -

#22 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 12 March 2019 - 06:47 PM

Eating some pistachios, just had an idea:

Can you try creating a small app with only those two blocks (other than the app framework), then draw the line to begin with. Leave your

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centreMapOnLocation(lastKnownLocation,"Your Location");
        } else {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
        }



SEE EDIT, this won't work: within a button click event to display all returns inside the code block above to EditText Text values. Don't have your initial Polyline depending on it AT ALL. Just leave it somewhere in the background. Assign another button to replace values inside the prebuilt independent line to your Polyline (or create another). That way, you can observe each step, and perhaps it will give us more insight on what's causing the line to disappear. Keep in mind that you can reassign Polyline parameters using many methods listed here:

https://developers.g.../model/Polyline

such as...

setPoints(List<LatLng> points)
//Sets the points of this polyline. 



so maybe rather than trying to just build the polyline based off your other methods you could build the polyline (as you said you already did) then try setting the points after it's already built.

Also, completely remove the conditional and do this work inside a button where we know that the app has permissions to your location. From there, we can start figuring out where the line disappearing issue is originating from. Additionally, you should be monitoring the output tab inside debug mode. Just because the app isn't crashing or throwing an exception doesn't mean there isn't going to be useful information from the IDE.

I'm still very curious; let me know.

EDIT: The EditText won't accept those returns, instead just try that method to set the points on an already created line.

This post has been edited by hexagod: 12 March 2019 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#23 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 13 March 2019 - 08:30 AM

Ok so after reading your post again, I think I misunderstood the issue... apologies, I'm extremely busy, so I can't devote a ton of time to this. To get this straight, your lines are drawing on the right coordinates, but when you try to center on location via this method:

centreMapOnLocation(lastKnownLocation,"Your Location");

..that's when the lines disappear?
Was This Post Helpful? 0
  • +
  • -

#24 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 31
  • View blog
  • Posts: 577
  • Joined: 29-October 16

Re: arrayadapter displaying wrong data + cant route in google map

Posted 13 March 2019 - 09:09 AM

Ok so I just installed visual studio code. Again, apologies about the sloppy responses. I have spent the last three years coding in an IDE so that kinda ruined me. It's really hard to review your code without my syntax markers, etc.. but I just noticed this:

        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(userLocation).title(title));



When you're calling centreMapOnLocation you're clearing the lines and at the end of that codeblock. Maybe you should .clear() before you add them?

void 	
clear()
//Removes all markers, >>>polylines<<<, polygons, overlays, etc from the map.



https://developers.g.../maps/GoogleMap

and that's coming after you add them, as far as I can tell.

This post has been edited by hexagod: 13 March 2019 - 09:11 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2