0

Estoy programando en android y necesito que mis marcadores siempre estén visibles en el título.

Este es el código que utilizo:

if ((latN != 0)&& lonN != 0){

    Marker marcador1 = mGoogleMap
                       .addMarker(new MarkerOptions()
                       .position(new LatLng(latN, lonN))
                       .title(dato.substring(0, dato.indexOf("$")))
                       .snippet("que es esto") 
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.negocio2)));


    marcador1.showInfoWindow();
}

Este código funciona muy bien pero sólo me muestra la información del último marcador.

1 respuesta 1

0

Resolví el problema, tenia que dibujar iconos que se parecieran a los marcadores este es el código que utilice:

package com.example.framac.app_framac;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdate;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.client.RedirectException;

public class MostrarMapa extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mostrar_mapa);
        // 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);


    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
/*
        Marker marcador = mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(-16.526988,-68.266685))
                        .title("MARITSA CONDORI")
                        .icon(BitmapDescriptorFactory.fromBitmap(setMarkerDrawable("142")))
                .snippet("FRENTE CASA AZUL")
        );

         mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(-16.559788,-68.916685))
                        .title("RUTH APAZA")
                        .icon(BitmapDescriptorFactory.fromBitmap(setMarkerDrawable("36598")))
                 .snippet("GARAJE ROJO RNO 980")

        );

        mMap.addMarker(new MarkerOptions()
                        .position(new LatLng(-16.526788,-68.216685))
                        .title("ROXANA CORO")
                        .icon(BitmapDescriptorFactory.fromBitmap(setMarkerDrawable("3605")))
                .snippet("PARED TIGO NRO 134")
        );
*/


        Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        String dato = extras.getString("PLIST");
        ObtDatos(dato);



    }

    public Bitmap setMarkerDrawable(String dato) {
        int background = R.drawable.marcadorcasa;

/* DO SOMETHING TO THE ICON BACKGROUND HERE IF NECESSARY */
/* (e.g. change its tint color if the number is over a certain threshold) */

        Bitmap icon = drawTextToBitmap(background, String.valueOf(dato));

        return icon;
    }

    public Bitmap setMarkerDrawable2(String dato) {
        int background = R.drawable.marcadorazul;

/* DO SOMETHING TO THE ICON BACKGROUND HERE IF NECESSARY */
/* (e.g. change its tint color if the number is over a certain threshold) */

        Bitmap icon = drawTextToBitmap(background, String.valueOf(dato));

        return icon;
    }

    public Bitmap drawTextToBitmap(int gResId, String gText) {
        Resources resources = getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
/* SET FONT COLOR (e.g. WHITE -> rgb(255,255,255)) */
        paint.setColor(Color.rgb(0, 0, 0));
/* SET FONT SIZE (e.g. 15) */
        paint.setTextSize((int) (12 * scale));
/* SET SHADOW WIDTH, POSITION AND COLOR (e.g. BLACK) */
        paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

        Rect bounds = new Rect();
        paint.getTextBounds(gText, 0, gText.length(), bounds);
        int x = (bitmap.getWidth() - bounds.width()) / 2;
        int y = (bitmap.getHeight() + bounds.height()) / 2;
        canvas.drawText(gText, x, y, paint);

        return bitmap;
    }


    public void CargarLista(ArrayList<String> datos) {
        double lat = 0, lon = 0, latD = 0, lonD = 0;
        String CodigoCliente = "", NombreCliente = "", RefCliente = "", telefonos = "", Op = "";
        // = null;

        for (int i = 0; i < datos.size(); i++) {
            String dato = datos.get(i).toString();

            try {
                CodigoCliente = String.valueOf(dato.substring(0, dato.indexOf("@")));
                NombreCliente = String.valueOf(dato.substring(dato.indexOf("@") + 1, dato.indexOf("$")));
                lat = Float.valueOf(dato.substring(dato.indexOf("$") + 1, dato.indexOf("%")));
                lon = Float.valueOf(dato.substring(dato.indexOf("%") + 1, dato.indexOf("&")));
                RefCliente = String.valueOf(dato.substring(dato.indexOf("&") + 1, dato.indexOf("*")));
                telefonos = String.valueOf(dato.substring(dato.indexOf("*") + 1, dato.indexOf("#")));
                Op = String.valueOf(dato.substring(dato.indexOf("#") + 1, dato.length()));

                //datos.get(0).equals("false")
                if (Op.equals("1")) {
                    Marker marcador = mMap.addMarker(new MarkerOptions()
                            .position(new LatLng(lat, lon))
                            .title(String.valueOf(NombreCliente))
                            .icon(BitmapDescriptorFactory.fromBitmap(setMarkerDrawable(CodigoCliente)))
                            .snippet(RefCliente + " Cel: " + telefonos)
                    );

                }

                if (Op.equals("2")) {
                    Marker marcador = mMap.addMarker(new MarkerOptions()
                            .position(new LatLng(lat, lon))
                            .title(String.valueOf(NombreCliente))
                            .icon(BitmapDescriptorFactory.fromBitmap(setMarkerDrawable2(CodigoCliente)))
                            .snippet(RefCliente + " Cel: " + telefonos)
                    );


                }


            } catch (ArrayIndexOutOfBoundsException excepcion) {
                lat = 0;
                lon = 0;

            }
            if ((lat != 0) && (lon != 0)) {
                latD = lat;
                lonD = lon;

            }

            //marcador.showInfoWindow();

        }


        CameraPosition camPos = new CameraPosition.Builder()
                .target(new LatLng(latD, lonD))   //Centramos el mapa en Madrid
                .zoom(15)         //Establecemos el zoom en 19
                .bearing(45)      //Establecemos la orientación con el noreste arriba
                .build();

        CameraUpdate camUpd3 =
                CameraUpdateFactory.newCameraPosition(camPos);

        mMap.animateCamera(camUpd3);

        //mMap.se
    }


    public void ObtDatos(String codigo){
        AsyncHttpClient client = new AsyncHttpClient();
        String url = "http://www.framac.com.bo/webservice/pListBuscarE.php";

        RequestParams parametros = new RequestParams();
        //parametros.put("codigo", 131);

        parametros.put ("codigo", codigo);
        //parametros.put ("dia", 2);

        client.post(url, parametros, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200){
                    CargarLista(ObtDatosJason(new String (responseBody)));
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }


    public ArrayList<String> ObtDatosJason(String response){
        ArrayList<String> listado = new ArrayList<String>();
        try{
            JSONArray jsonArray = new JSONArray(response);
            String texto;
            for (int i=0;i<jsonArray.length();i++){
                texto = jsonArray.getJSONObject(i).getString("codigo")+ '@'+
                        jsonArray.getJSONObject(i).getString("cliente")+ '$'+
                        jsonArray.getJSONObject(i).getString("latitud")+ '%'+
                        jsonArray.getJSONObject(i).getString("longitud") + '&' +
                        jsonArray.getJSONObject(i).getString("referencia") + '*' +
                        jsonArray.getJSONObject(i).getString("telefonos") + '#' +
                        jsonArray.getJSONObject(i).getString("entrega");


                listado.add(texto);
            }


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

        return  listado;
    }

}

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.