访问onMarkerClick事件到布局元素

时间:2017-01-04 11:19:38

标签: android android-layout layout

  

他是我在这个网站上的第一个问题。我想知道是否在onMarker方法中,如果可能的话,更改线性布局的可见性元素。

这是我的活动,如果您使用按钮的事件单击,您可以更改元素的可见性。但是如果你使用onMarkerClick我无法改变它。我不知道它是否符合上下文。这样做有可能吗?抱歉,我的英文。

  package com.example.ivan.myapplicationmaps;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.io.UnsupportedEncodingException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import android.os.AsyncTask;
import org.json.JSONObject;

import android.view.Menu;


public class MainActivityDos extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener, View.OnClickListener {

    private GoogleMap mMap;

    // variables nuevas
    private Button url_ruta;

    //hasta aquí variables nuevas

    private String origin;
    private String destination;
    private String point1;
    private String point2;

    //array de coordenadas

    private String coordiantes;
    private int n_points;
    private String query;

    private LinearLayout layoutBar, layoutButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dos);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        layoutBar = (LinearLayout)findViewById(R.id.progressBarLayout);
        layoutButton = (LinearLayout)findViewById(R.id.buttonsAudio);


        url_ruta = (Button) findViewById(R.id.urlRuta);
        url_ruta.setOnClickListener(this);

        //coger lo que se envía en el intent;

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras(); // estoy puede estar vacío

        if(bundle != null){
            //String rutaUrl = bundle.get("URL_RUTA").toString();

            coordiantes = (bundle.get("COORDINATES").toString());
            query = (bundle.get("QUERY").toString());
            n_points = Integer.parseInt(bundle.get("POINTS").toString());

            url_ruta.setText(query);
        }

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng OriginPoint = new LatLng(40.408272, -3.694506);

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(OriginPoint, 16));
        googleMap.setOnMarkerClickListener(this);
        //mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mMap.setMyLocationEnabled(true);

        sendRequest();

    }


    @Override
    public boolean onMarkerClick(Marker marker) {

        if(marker.getTitle().equals("punto1")){
            Toast.makeText(getApplicationContext(), "hola" , Toast.LENGTH_LONG).show();

            //layoutButton.setVisibility(View.VISIBLE); //this is no working.
            //cambiarLayout();
            //layoutBar.setVisibility(View.VISIBLE);

        }

        return false;
    }


    public void sendRequest(){

        try{
            //String url = createUrl();

            DownloadTask downloadTask = new DownloadTask(mMap,coordiantes);

            downloadTask.execute(query);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        layoutButton.setVisibility(View.VISIBLE); // this is working
        layoutBar.setVisibility(View.VISIBLE);
    }
}

//--------------------XML


<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_dos"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.ivan.myapplicationmaps.MainActivityDos"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter destination address"
        android:id="@+id/urlRuta" />


    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/progressBarLayout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:weightSum="1"
        android:layout_above="@+id/buttonsAudio"
        android:layout_alignParentStart="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:background="@color/wallet_bright_foreground_disabled_holo_light"
        android:visibility="invisible">

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/progressBar"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:max="100"
            android:progress="0"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/buttonsAudio"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/wallet_bright_foreground_disabled_holo_light"
        android:visibility="invisible">

        <Button
            android:text="Play"
            android:id="@+id/buttonFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <TextView
            android:text="Museo reina sofia y atocha refe"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/textView"
            android:textSize="20sp"
            android:layout_weight="1"
            android:layout_marginLeft="5dp" />

    </LinearLayout>

</RelativeLayout>

0 个答案:

没有答案