Baidu map based on Android realizes the return of longitude and latitude information of input address

1 solution 1

The solution here is from Netizens article , for the input address information requirements: city name + specific address name.

 

If the input address information only has a specific address name and no city name, the longitude and latitude information may not be resolved. There is also the analysis of the longitude and latitude and the reverse analysis shows that there will be obvious deviation when it is used as a landmark mark on the map. The actual geographical distance of the deviation is about one kilometer... This is the result of the owner's own actual operation on the computer. It can't be completely sure whether this is the case. Another point is that the application may flash back if it is analyzed. The result of my experiment will flash back once and restart the application for the second time, then the correct analysis result will appear.

 

Finally, say the latitude and longitude coordinates, the final numerical results are divided by the sixth power of 10 (ie 1e6), otherwise, an error will be reported when the reverse resolution is to landmark. In addition, if multiple addresses are resolved in the listening method at the same time, it may always flash back. Therefore, the following code is for reference only.

 

Specific implementation method:

public GeoPoint getGeoPointBystr(String str) {
        GeoPoint gpGeoPoint = null;
        if (str!=null) {
            Geocoder gc = new Geocoder(getActivity(), Locale.CHINA);
            List<Address> addressList = null;
            try {
                addressList = gc.getFromLocationName(str, 1);
                if (!addressList.isEmpty()) {
                    Address address_temp = addressList.get(0);
                    //Calculate latitude and longitude
                    double Latitude=address_temp.getLatitude()*1E6;
                    double Longitude=address_temp.getLongitude()*1E6;
                    System.out.println("longitude:"+Latitude);
                    System.out.println("latitude:"+Longitude);
                    //production GeoPoint
                    gpGeoPoint = new GeoPoint((int)Latitude, (int)Longitude);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return gpGeoPoint;
    }

 

 

Call the above method to resolve the latitude and longitude:

 GeoPoint result = getGeoPointBystr("South Sanmen, Zhongnan University for nationalities, Wuhan");

 Double  num_latitude = (result.getLatitudeE6()) / 1e6;
 Double  num_longitude = (result.getLongitudeE6()) / 1e6;

Tags: Android IE

Posted on Tue, 31 Mar 2020 15:26:40 -0700 by Nuv