从地址中查找纬度和经度

时间:2022-11-23 08:35:55

I am trying to display markers on map in android by using address.
This is my code:

我试图通过使用地址在android中的地图上显示标记。这是我的代码:

public void onMapReady(GoogleMap googleMap){
    mMap = googleMap;
    Geocoder coder= new Geocoder(this);
    try
    {
        String straddress = "155 Park Theater,Palo Alto,CA";
        Double latitude = 0.0;
        Double longitude = 0.0;
        List<Address> addresses = coder.getFromLocationName("155 Park Theater,Palo Alto,CA",1);
        Address location = addresses.get(0);
        LatLng p1 = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(p1).title("California"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(p1));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

If I do not put street number, then it will display the marker. But, it will just display marker on the street. I want marker on particular location.

如果我没有输入街道号码,那么它将显示标记。但是,它只会在街上显示标记。我想要特定位置的标记。

I am getting following error while inputting the whole address with street number. The "address" list is empty.

输入带有街道号码的整个地址时出现以下错误。 “地址”列表为空。

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

1 个解决方案

#1


0  

Update your code with following :

使用以下代码更新代码:

 List<Address> address;
 LatLng latLng = null;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    latLng = new LatLng(location.getLatitude(),location.getLongitude());
    }

#1


0  

Update your code with following :

使用以下代码更新代码:

 List<Address> address;
 LatLng latLng = null;

try {
    address = coder.getFromLocationName(strAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    latLng = new LatLng(location.getLatitude(),location.getLongitude());
    }