How to route Gmaps with shapes/polygons

Asked

Viewed 596 times

4

I will need to manipulate a map ( preferably Gmaps ) containing the mesoregions of each state. I found a map of mesoregions created from Gmaps, but it doesn’t allow interactions like creating routes, etc.

How to join the two characteristics in a single map?

  • If you can extract this map and understand how it works. You could calculate the route normally and then "plot" in this map style. Just a hunch...

  • Post what you’ve already done. It’s javascript?

  • Yes, javascript. @Guilhermenascimento

2 answers

2


If you want to trace routes as a hint of a path from one place to another, whether by car, bus, or other means of transport, you should not use shapes/polygons, but the Directions service api, which is already available in your script because you are referencing google maps. With this api you can pass two positions in latidude and longitude or passing the names of the places (in this case specifying in which language the names are) and it will return a list of Latlng points with the suggested path. You can also pass up to eight intermediate points wherever the path passes. There with this list you draw a Polyline on the maps, it’s easy. Directions Service

If you want to use the shapes and polygons to draw a drawing over the regions, you will have to recover several border positions and assemble the shapes in the same hand. You have some interesting answers to that here

0

see if this can help you:

//-- Classe responsável em buscar os pontos de rota (Curva a Curva) do Google
private class getRoute extends AsyncTask<Void, Void, Void>
{
    //-- Método de Execução da Task
    @Override
    protected Void doInBackground(Void... parametro)
    {           
        HttpGet http = new HttpGet("https://maps.google.com/maps?output=json&saddr="+_start+"&daddr="+_end);

        HttpClient httpclient = new DefaultHttpClient();

        HttpResponse response = null;
        try
        {
            response = httpclient.execute(http);
        }
        catch(ClientProtocolException e1)
        {
            e1.printStackTrace();
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }

        HttpEntity entity = response.getEntity();

        if(response.getStatusLine().getStatusCode() == 200)
        {
            if(entity != null)
            {
                InputStream instream = null;
                try
                {
                    instream = entity.getContent();
                }
                catch(IllegalStateException e)
                {
                    e.printStackTrace();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
                String resultString = converterStreamEmString(instream);
                try
                {
                    instream.close();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }

                String regex = "points:\\\"([^\\\"]*)\\\"";
                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(resultString);
                if(m.find())
                {
                    obterPontos(m.group(1));
                }
            }
        }
        return null;
    }


    //-- Converte o binario para double
    private void obterPontos(String str)
    {
        _lat = new ArrayList<Double>();
        _lon = new ArrayList<Double>();

        str = str.replace("\\\\", "\\");

        int index = 0, len = str.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do
            {
                b = str.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            }
            while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

           _lat.add(lat*1E-5);

            shift = 0;
            result = 0;
            do
            {
                b = str.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            }
            while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            _lon.add(lng*1E-5);

            // Usando a lat e lng acima, preencha o objeto que representa 
            // um ponto no mapa (varia de acordo com a API de mapas)
            // e adicione a sua coleção de pontos
        }
    }
}

    //-- Converte o Stream recebido do servidor Google para String
    private String converterStreamEmString(InputStream is)
    {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        int a;
        try
        {
            while ((a = is.read()) != -1)
            { 
                buffer.write(a);
            }
            buffer.close();

        return new String(buffer.toByteArray());

        }
        catch (IOException e)
        {
            e.printStackTrace();
            return e.toString();
        }
    }

Browser other questions tagged

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