2014年5月19日 星期一

Android Location In China

We all know about the location positioning in China will suffer from the Googla Maps API. If we get the location from GPS or network, we have to correct the location from the map. Or course, you will say that I got the location from GPS. Does the location is incorrect? The answer will be yes. The GPS location is correct, but the map is not correct. So..How to fix it?

I have found the answer the this question for a long time. You can find many solutions from google. But!! but...How do I know a positioning is in CHINA??????

Finally, I use the solution in my Android system.

    public static String getCountryCodeFromLocation(Context ctx, Location l) {
        Geocoder geocoder = new Geocoder(ctx, Locale.US);
        List
addresses = null;
        try {
            addresses = geocoder.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
        } catch (IOException e1) {
            DebugMode.Log(TAG, "IO Exception in getFromLocation()");
            e1.printStackTrace();
        } catch (IllegalArgumentException e2) {
            String errorString = "Illegal arguments " + Double.toString(l.getLatitude()) + " , " + Double.toString(l.getLongitude()) + " passed to address service";
            DebugMode.Log(TAG, errorString);
            e2.printStackTrace();
            return errorString;
        }
       
        if(addresses != null && addresses.size() > 0) {
            // Get the first address
            Address address = addresses.get(0);
            return address.getCountryCode();
        } else {
            return "No address found";
        }
    }


If the country is "CN", the address is in China. That solved my problem...

沒有留言: