Android Geocode:几个小时后,Geocode无法识别越来越多的城市名称

时间:2019-03-20 07:26:04

标签: android google-maps geocode

我正在使用地理编码来获取城市名称字符串数组的LatLng。问题是,首先,所有城市的名称都很好,但是几个小时后,它并没有首先获得一个城市,然后又获得了两个……现在我被四个城市所识别。我正在将城市名称更改为原始坐标旁边的一个小城市,但是问题仍然存在,而且还在不断增加。我留给我我的MapsActivity和我的地理编码:

(我已经不得不由Badalona改变巴塞罗那,由Azucaica改变托莱多,现在我遇到了塞维利亚和莱昂的麻烦) MapsActivity:

package dam.javazquez.tuvotocuenta.util;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

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.MarkerOptions;

import java.io.IOException;

import dam.javazquez.tuvotocuenta.R;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    String[] provincias = new String[]{"Álava", "Albacete", "Alicante", "Almería", "Asturias", "Ávila", "Badajoz",
            "Badalona", "Burgos", "Cáceres", "Cádiz", "Cantabria", "Castellón", "Ciudad Real", "Córdoba",
            "La Coruña", "Cuenca", "Gerona", "Granada", "Guadalajara", "Guipúzcoa", "Huelva", "Huesca",
            "Islas Baleares", "Jaén", "León", "Lérida", "Lugo", "Madrid", "Málaga", "Murcia", "Navarra", "Orense",
            "Palencia", "Las Palmas", "Pontevedra", "La Rioja", "Salamanca", "Segovia", "Sevilla", "Soria",
            "Tarragona", "Santa Cruz de Tenerife", "Teruel", "Azucaica", "Valencia", "Valladolid", "Vizcaya",
            "Zamora", "Zaragoza"};
    private GoogleMap mMap;

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

    /*
    PASOS:
    1. Geocodizar todas las capitales de provincia, recorriendo el array de ciudades
    2. En cada iteración, crear un marker en la localización
    3. Hacer la consulta de agrupamiento por partido en una ciudad, ordenarlos de mayor a menor count
     */

    /**
     * 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;
        LatLng latLng = new LatLng(40.4167, -3.70325);
        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

        for (String provincia : provincias) {
            try {
                String[] parts = Geocode.getLatLong(this, provincia).split(",");
                String lat = parts[0];
                String lon = parts[1];
                LatLng loc = new LatLng(Double.parseDouble(lat), Double.parseDouble(lon));
                if (provincia.equals("Sevilla") || provincia.equals("Huelva") || provincia.equals("Jaén")) {
                    mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.psoe)).title(provincia));
                } else if (provincia.equals("Álava") || provincia.equals("Guipúzcoa") || provincia.equals("Vizcaya") || provincia.equals("Tarragona") || provincia.equals("Badalona")) {
                    if (provincia.equals("Badalona")) {
                        mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.podemos)).title("Barcelona"));
                    }
                    mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.podemos)).title(provincia));
                } else if (provincia.equals("Gerona") || provincia.equals("Lérida")) {
                    mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.erc)).title(provincia));
                } else if (provincia.equals("Navarra")) {
                    mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.upn)).title(provincia));
                } else {
                    if (provincia.equals("Azucaica")) {
                        mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.pp)).title("Toledo"));
                    } else {
                        mMap.addMarker(new MarkerOptions().position(loc).icon(BitmapDescriptorFactory.fromResource(R.drawable.pp)).title(provincia));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 5));
    }
}

地理编码:

public class Geocode {

    public static String getLatLong(Context ctx,String address) throws IOException {
        Geocoder geocoder = new Geocoder(ctx);
        double latitude = 0;
        double longitude = 0;
        List<Address> addresses;
        addresses = geocoder.getFromLocationName(address, 1);
        if(addresses.size() >= 0) {
            latitude= addresses.get(0).getLatitude();
            longitude= addresses.get(0).getLongitude();
        }

        String result = latitude +","+ longitude;
        return result;
    }
}

非常感谢您!

0 个答案:

没有答案