arrayadapter displaying wrong data + cant route in google map

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 Bobby_Bubbles   User is offline

  • D.I.C Regular

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

arrayadapter displaying wrong data + cant route in google map

Posted 11 March 2019 - 06:46 AM

Lemme slap my code right here...

public class Index extends AppCompatActivity implements GoogleMap.OnMyLocationButtonclickListener, GoogleMap.OnMyLocationclickListener, OnMapReadyCallback, ActivityCompat.OnRequestPermissionsResultCallback {
    public static String[] account;

    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    private boolean mPermissionDenied = false;
    private GoogleMap mMap;
    String[][] mobileArray = {
            new String[] {"150 Goyeau St, Windsor, ON N9A 6J5","Product 1","N/A","3:30pm"},
            new String[] {"2000 Talbot Road West, Windsor, ON N9A 6S4","Product 2","N/A","4:00pm"},
            new String[] {"350 City Hall Square W, Windsor, ON N9A 6S1","Product 3","N/A","4:30pm"}
    };

    @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);
    }

    @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);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[0]);
        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){
                tVSpecial.setText(mobileArray[position][2]);
                tVProduct.setText(mobileArray[position][1]);
            }
        });
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        mMap.setOnMyLocationButtonclickListener(this);
        mMap.setOnMyLocationclickListener(this);
        enableMyLocation();
    }
    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 boolean onMyLocationButtonclick() {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        return false;
    }
    @Override
    public void onMyLocationclick(@NonNull Location location) {
        Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
    }
    @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");
    }
}



I am attempting to plot a path in google maps (and automatically zoom in my location when activity is loaded.) by tapping an item in the array.

Problem being is that the arrayadapter is listing the first arrays items. Programmatically it makes sense but its not what i want. I wanted to display the first item in the second array for all first arrays. Meaning i want just the address to display in the arrayadapter.

i know the problem lies right here.

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[0]);



But what part of the array do i call to have these displayed correctly?

Second question is how can i zoom into my location when this activity is loaded and how can i plot the path? I tried 2 different ways and one of them had errors that prevented it from working and the other didnt display it at all.

Here is my layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/in_the_weeds"
    android:layout_height="match_parent">

    <include
        layout="@layout/map"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginBottom="0dp" />

    <TextView
        android:id="@+id/tVNull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="25dp"
        android:background="#80FFFFFF"
        android:text="Driver"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/tVDriver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="20dp"
        android:background="#80FFFFFF"
        android:text="No Driver"
        android:textSize="24sp"/>

    <ListView
        android:id="@+id/lVDelivery"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#80FFFFFF"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="90dp" />

    <TextView
        android:id="@+id/tVProductNull"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginTop="90dp"
        android:layout_marginEnd="20dp"
        android:layout_toRightOf="@+id/lVDelivery"
        android:background="#80FFFFFF"
        android:text="Product"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tVProduct"
        android:layout_width="160dp"
        android:layout_height="165dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="125dp"
        android:layout_marginEnd="20dp"
        android:layout_toRightOf="@+id/lVDelivery"
        android:background="#80FFFFFF"
        android:textSize="24sp"
        android:text="1g - Bubba Hash" />

    <TextView
        android:id="@+id/tVSpecial"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="300dp"
        android:layout_toRightOf="@+id/tVProductNull"
        android:background="#80FFFFFF"
        android:textSize="24sp"
        android:text="TextView" />

</RelativeLayout>



Is This A Good Question/Topic? 0
  • +

Replies To: arrayadapter displaying wrong data + cant route in google map

#2 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 - 08:16 AM

Could you just change 0 to a 1?

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[1]);
Was This Post Helpful? 0
  • +
  • -

#3 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 - 08:25 AM

Oh I just saw you're trying to grab only the first item from that array. Have you tried

ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listview, mobileArray[1][0]);

I also noticed this in the android documentation:

Quote

By default, the array adapter creates a view by calling Object.toString() on each data object in the collection you provide, and places the result in a TextView.


"on each"
Was This Post Helpful? 0
  • +
  • -

#4 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 - 08:33 AM

no because it would just take the info in the secon index in the mobile array.

This was resolved by simply incorporating a loop

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);



Issue was resolved.

Now the second part is dealing with google maps. I cannot get this working for the life of me. What im trying to do is when i click on the array item it would pull the address info and plot my course to that from my location to that location. But i can never get it to work at all. I have directions api and maps api enabled on my google account for using the API key. The map shows up and when i hit the crosshair logo it goes to my position but i want it done automatically whenever i load the activity.

This is the big part.
Was This Post Helpful? 0
  • +
  • -

#5 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 - 08:36 AM

Quote

Polylines

The Polyline class defines a set of connected line segments on the map. A Polyline object consists of a set of LatLng locations, and creates a series of line segments that connect those locations in an ordered sequence.

This video gives ideas on how to help your users get to where they're going, using polylines to draw a path on the map.

To create a Polyline, first create a PolylineOptions object and add points to it. Points represent a point on the earth's surface, and are expressed as a LatLng object. Line segments are drawn between points according to the order in which you add them to the PolylineOptions object. To add points to a PolylineOptions object, call PolylineOptions.add(). Notice that this method takes a variable number of parameters so you are able to add multiple points at a time (you can also call PolylineOptions.addAll(Iterable<LatLng>) if the points are already in a list).

You can then add the polyline to a map by calling GoogleMap.addPolyline(PolylineOptions). The method returns a Polyline object with which you can alter the polyline at a later time.

The following code snippet illustrates how to add a rectangle to a map:

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

To alter the shape of the polyline after it has been added, you can call Polyline.setPoints() and provide a new list of points for the polyline.

You can customize the appearance of the polyline both before adding it to the map and after it has been added to the map. See the section on customizing appearances below for further details.

Polyline events

By default, polylines are not clickable. You can enable and disable the clickability by calling Polyline.setClickable(boolean).

Use an OnPolylineClickListener to listen to click events on a clickable polyline. To set this listener on the map, call GoogleMap.setOnPolylineClickListener(OnPolylineClickListener). When a user clicks on a polyline, you will receive an onPolylineClick(Polyline) callback.
Note: If multiple overlays or shapes (markers, polylines, polygons, circles and/or ground overlays) are overlaid on top of each other, the click event is cycled through the cluster of markers first, then triggered for other clickable overlays or shapes based on their z-index values. At most one event is triggered per click. In other words, the click is not passed down to the overlays or shapes with lower z-index values. Read more about marker z-index and click events.


https://developers.g...roid-sdk/shapes

Sample

ok well your question was kind of confusing "I wanted to display the first item in the second array for all first arrays." lol
Was This Post Helpful? 0
  • +
  • -

#6 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 - 08:58 AM

Where in that massive code block is your attempt at centering the map? .. please don't just drop a massive code block. I'm looking now for something similar to OnPageFinished (which works on webviews).

EDIT: actually I do like having the whole block but narrow it down for me a bit. I don't see any attempt to center the map.
Was This Post Helpful? 0
  • +
  • -

#7 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 - 09:06 AM

CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build() googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));



Quote

mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;

// For showing a move to my location button
googleMap.setMyLocationEnabled(true);

// For dropping a marker at a point on the Map
LatLng sydney = new LatLng(-34, 151);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));

// For zooming automatically to the location of the marker
CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
});

return rootView;
}


https://stackoverflo...using-viewpager

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

Was This Post Helpful? 0
  • +
  • -

#8 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 - 09:18 AM

Sorry about the confusion i did word that bad.

Im attempting to get the starting point to where im at and this is what i got so far.

@Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
        mMap.setOnMyLocationButtonclickListener(this);
        mMap.setOnMyLocationclickListener(this);
        LatLng start = new LatLng(42.3149, 83.0364);
        mMap.addMarker(new MarkerOptions().position(start).title("Windsor"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(start));
        enableMyLocation();
    }



But this is dropping me in china.

Windsor ontario is where im currently at...

https://www.google.c...chrome&ie=UTF-8
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

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

Posted 11 March 2019 - 09:20 AM

Try -83.0364
Was This Post Helpful? 0
  • +
  • -

#10 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 - 09:41 AM

Here's something else that you may find useful. It's a method for finding Lat and Lang from an address with the Google API

public GeoPoint getLocationFromAddress(String strAddress){

Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    location.getLatitude();
    location.getLongitude();

    p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
                      (double) (location.getLongitude() * 1E6));

    return p1;
    }
}


https://stackoverflo...de-from-address
Was This Post Helpful? 0
  • +
  • -

#11 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 - 11:58 AM

got the location working...but now im trying to parse directions from a link

https://maps.googlea...=494+Aylmer+Ave,+Windsor,+ON+N9A+1T6&destination=City+Hall+Square+E,+Windsor,+ON&key=AIzaSyCDNJYVNRlDzg7QiZsyFVuKsaKKjpFCEB8

If you want a visual representative

https://www.google.c...y+Hall+Square+E,+Windsor,+ON/42.3179745,-83.0290536/@42.3171694,-83.0338517,17z/data=!3m1!4b1!4m9!4m8!1m5!1m1!1s0x883b2ce04b72dc87:0x9ac1b86418024e6f!2m2!1d-83.0347256!2d42.3177281!1m0!3e0

im trying to parse this data into a JSONArray by doing this

try {
                    JSONObject jresponse = new JSONObject(result);
                    JSONObject routes = jresponse.getJSONObject("routes");
                    //Toast.makeText(context, "JSON "+routes, Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    Toast.makeText(context, "Unknown gmaps error "+e, Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }



im getting a jsonexception error but....its still displaying it in my toast...

how can i pull the data from just the steps array? I cant figure out how to pull that info from there.
Was This Post Helpful? 0
  • +
  • -

#12 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 - 01:13 PM

was able to pull the right information now.

JSONArray routes = json.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");



But i need some serious help to just put in polylines. I am having a lot of struggle with this because its just not showing up.

i put this in...

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
                .clickable(true)
                .add(new LatLng(42.3149, 83.0364),
                        new LatLng(42.3183, 83.0373)));
        Intent intent = getIntent();
        if (intent.getIntExtra("Place Number",0) == 0 ){
            // Zoom into users location
            locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            locationListener = new LocationListener() {
                @Override
                public void onLocationchanged(Location location) {
                    centreMapOnLocation(location,"Your Location");
                }

                @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);
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#13 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 - 01:22 PM

you're going to have to be a little more specific than "jsonexception error." and that code you posted doesn't have anything to do with parsing a string. Show me what you have tried
Was This Post Helpful? 0
  • +
  • -

#14 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 - 01:50 PM

the json exception was resolved from above post.

i was able to pull the steps array into the JSON result. so that part is done.

the issue now is plotting the route to get to my destination on google maps. please look above at my attempt to plot a fixed coordinate.

I reviewed the api examples but its extremely confusing and overwhelming. im looking for something simple.
Was This Post Helpful? 0
  • +
  • -

#15 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 - 02:31 PM

I don't see any glaring errors in your code. It looks the same as the android documentation and all of the other examples that I've seen. Maybe you could try instantiating the polyline options with color and width methods for setting those properties?

 Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
     .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
     .width(5)
     .color(Color.RED));



Also, is the google map centered over those points? Are you sure you're looking at the right part of the map? Afaik the method for adding polylines has nothing to do with centering the map. They are two separate processes.

This post has been edited by hexagod: 11 March 2019 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2