How to open the Google Maps app via Intent at a given Latitude/Longitude?

Asked

Viewed 1,588 times

1

Using this rule I can view the database information. I want to put a button to get the latitude and longitude of the database for when click on it open google maps.

public class UsuarioAdapter extends ArrayAdapter<Usuario> {
    private Context context;
    private ArrayList<Usuario> lista;

    public UsuarioAdapter(Context context, ArrayList<Usuario> lista){
        super(context,0,lista);
        this.context = context;
        this.lista = lista;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Usuario itemPosicao = this.lista.get(position);

        convertView = LayoutInflater.from(this.context).inflate(R.layout.item_lista,null);
        final View layout = convertView;

        TextView textViewNome = (TextView) convertView.findViewById(R.id.textViewNome);
        textViewNome.setText(itemPosicao.getNome());

        TextView textViewCrm = (TextView) convertView.findViewById(R.id.textViewCrm);
        textViewCrm.setText(itemPosicao.getCrm());

        TextView textViewTelefone = (TextView) convertView.findViewById(R.id.textViewTelefone);
        textViewTelefone.setText(itemPosicao.getTelefone());

        TextView textViewConvenios = (TextView) convertView.findViewById(R.id.textViewConvenios);
        textViewConvenios.setText(itemPosicao.getConvenios());


    TextView textViewLatitude = (TextView) convertView.findViewById(R.id.textViewLatitude);
    textViewLatitude.setText(itemPosicao.getLatitude());

    TextView textViewLongitude = (TextView) convertView.findViewById(R.id.textViewLongitude);
    textViewLongitude.setText(itemPosicao.getLongitude());

        Button button = (Button)convertView.findViewById(R.id.buttonComoChegar);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
        return convertView;
    }
}

How can I do this?

  • 1

    What is your difficulty? Fetch this data from the BD, open Google Maps or both?

  • both, wanted to open the button searching the latitude and longitude data that are in the table of the flock (table = longitute) and (table = latutide) and opening the (app) of google maps.

  • Where you are retrieving data regarding "Name", "Crm", "Telephone", etc?

  • from my local postgresql database

  • Then why not also include latitude and longitude in the array that passes to the Adapter?

1 answer

2


Latitude and longitude available in class User such data may be obtained in the same way as "Name", "Crm", "Telephone", etc.
Then just create the Intent to launch the Google maps.

    .......
    .......
    double final latitude = itemPosicao.getLatitude();
    double final longitude = itemPosicao.getLongitude();

    Button button = (Button)convertView.findViewById(R.id.buttonComoChegar);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String strUri = "http://maps.google.com/maps?q=loc:" + 
                             latitude + "," + longitude;
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                                       Uri.parse(strUri));

            intent.setClassName("com.google.android.apps.maps",
                                "com.google.android.maps.MapsActivity");

            context.startActivity(intent);

        }
    });

It is also possible to use an Intent built like this:

Uri mapAppUri = Uri.parse("geo:" + latitude + ","
        + longitude + "?q="
        + latitude + ","
        + longitude
        + "(" + Uri.encode("nome do local") + ")");
Intent intent = new Intent(Intent.ACTION_VIEW, mapAppUri);

The advantage is that if there are other installed applications that can handle locations they will be presented to the user to choose one.

  • Thanks for the ramaral help, everything went right here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.