Search code examples
androidgoogle-mapsandroid-intentlocale

Google Maps automatically convert period (.) into comma (,) from my Intent URI


I'm calling Google Maps Intent from my activity with this code found on StackOverflow:

    final String uriContent = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%s", pCoordinate);
    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
    pContext.startActivity(intent);

where pCooriante contains entirely address such as 1.23456,7.8901.

It works well when my phone is using English as its language, but when I change it to French or Vietnamese (which use comma , as its number decimal seperator), it can't work anymore, because the query proceeded by Google Maps look like: 1,000,2,000 (it is shown in the search bar, and after that, a message like Cannot find 1,0000,2,0000 appears), although the exact URI I sent to the intent is 1.000,2.000 (the coordinate is converted to String to prevent Locale problems, and therefore the Locale.ENGLISH in String.format is more or less just abundant).

In short, Uri.parse(uriContent) return exactly the request with the query is 1.000,2.000, but Google Maps itself changed it. However, the code for direction works well with either Locale:

    final String uriContent = String.format(Locale.ENGLISH, "google.navigation:q=%s", pCoordinate);
    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
    pContext.startActivity(intent);

Is there anyway to prevent the conversion of Google Maps? If I use geo:<coordinate>, it's fine, but I need a marker at that position.

Addional information:

This code final String uriContent = String.format("geo:0,0?q=%s&z=19", pCoordinate); doesn't work too, the periods are converted into commas.

This code final String uriContent = String.format("geo:%s?q=%s&z=19", pCoordinate, pCoordinate); can bring the map center to the coordinate, but still cannot put the marker there, and with the error "Cannot find 'coordinate with periods replaced by commas'"


Solution

  • I am using a temporary solution to this problem, by converting the decimal form of coordinates to degree one. (For example, instead of sending 10.768717,106.651488, I send 10° 46' 7.3812",106° 39' 5.3568"). The conversion is just simple mathematics operation.

    However, there was a problem with Java float and double precision, and that was a lot of distance when sending to Google Maps. Therefore I change my input data, convert data using C#'s decimal and my Android app just use it without manupilating anything. Here is the convesion (C#) code:

        protected String convertDecimalToDegree(decimal pDecimal)
        {
            int degree = (int)Math.Floor(pDecimal);
            pDecimal -= degree;
    
            pDecimal *= 60;
            int minute = (int)Math.Floor(pDecimal);
            pDecimal -= minute;
    
            pDecimal *= 60;
    
            return degree + "° " + minute + "\' " + pDecimal + "\"";
        }
    

    Usage:

                        String[] coordinates = shop.MapCoordination.Split(',');
                        decimal n1 = Decimal.Parse(coordinates[0]);
                        decimal n2 = Decimal.Parse(coordinates[1]);
                        shop.MapCoordination = this.convertDecimalToDegree(n1) + "," + this.convertDecimalToDegree(n2);
    

    I will mark this as answer for now, but I appreciate any solution without having to convert to this form.