如何使用地理位置的纬度和经度值移动到该位置

时间:2019-02-19 16:00:22

标签: google-maps search geolocation

我当前在我的应用程序的搜索栏中使用地理位置。搜索位置后,我得到了地理位置信息。

但是我想使用返回的纬度和经度值来使地图导航到地图上的该位置。

我用于地理定位的代码如下

private void geoLocate(){ Log.d(TAG, "geoLocate: geolocating");

    String searchString = mSearchText.getText().toString();

    Geocoder geocoder = new Geocoder(SearchActivity.this);
    List<Address> list = new ArrayList<>();

    try{
        list = geocoder.getFromLocationName(searchString, 1);
    }catch (IOException e){
        Log.e(TAG, "geoLocate: IOException: " + e.getMessage() );
    }

    if(list.size() > 0){
        Address address = list.get(0);
        ;

        Log.d(TAG, "geoLocate: found a location: " + address.toString());
        Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();

    }
}`

例如搜索曼彻斯特,我得到了结果,但我只想使用其经度和纬度值在地图上导航到它

D/SearchActivity: geoLocate: geolocating D/SearchActivity: geoLocate: found a location: Address[addressLines=[0:"Manchester, UK"],feature=Manchester,admin=England,sub-admin=Greater Manchester,locality=Manchester,thoroughfare=null,postalCode=null,countryCode=GB,countryName=United Kingdom,hasLatitude=true,latitude=53.480759299999995,hasLongitude=true,longitude=-2.2426304999999997,phone=null,url=null,extras=null]

1 个答案:

答案 0 :(得分:0)

添加到geolocate并起作用了:)

moveCamera(new LatLng(address.getLatitude(), address.getLongitude()), DEFAULT_ZOOM);