2

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'"

Luke Vo
  • 17,859
  • 21
  • 105
  • 181

4 Answers4

2

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.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • Please share! I am facing the exact same issue as you and you could help me a lot by sharing your info. – A. Steenbergen Oct 11 '13 at 16:06
  • I solved the problem by using an address instead of geocoordinates. This works perfectly in my scenario, although it may not be what you need if you need to navigate to certain positions or dont have internet access to get the address from the geocoder. – A. Steenbergen Oct 11 '13 at 16:14
  • Get the address corresponding to coordinates like in this example http://stackoverflow.com/a/9629434/923441 – A. Steenbergen Oct 11 '13 at 16:15
  • I don't think I should rely on this solution, how can you know the address corresponding to coordinates is correct? What if its parser also has this problem? – Luke Vo Oct 13 '13 at 09:03
0

You can use the following snippet to solve the problem

public String getCoordinates(String coordinates){
    if(Locale.FRANCE == Locale.getDefault()){
        Pattern p = Pattern.compile(",.*?(,)");
        Matcher m = p.matcher(coordinates);
        if (m.find( )) {
            int index = m.end(); //gets the second comma position
            String str1 = coordinates.substring(0,index-1);
            String str2 = coordinates.substring(index,coordinates.length());
            NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
            try {
                str1 = nf.parse(str1).toString();
                str2 = nf.parse(str2).toString();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return str1+","+str2;
        }
    }
    return coordinates;
}
Sunil Mishra
  • 3,796
  • 1
  • 27
  • 40
  • That's not the problem. When I send the coordiante, it follows correct pattern such as `1.234,1.234` as expected, but Google Maps parses the intent wrong itself, not me. – Luke Vo Sep 05 '13 at 14:50
  • If you pass latitudes in proper format, it shows properly on the map. I tried on my system by changing language to french. – Sunil Mishra Sep 05 '13 at 15:11
  • Which version of Google Maps are you using? – Luke Vo Sep 06 '13 at 05:16
  • I am testing on emulator, version 5.12.0 – Sunil Mishra Sep 06 '13 at 05:26
  • I think the problem only occurs with version 7. I am testing with Google Maps 7.1.0 on Android 4.0.4. – Luke Vo Sep 06 '13 at 06:33
  • can you try by using the function and passing the co-ordinates, without using `String.format` – Sunil Mishra Sep 06 '13 at 07:14
  • I tried, I even used a hardcoded String. The intent itself is perfectly fine (its URI is `geo:0,0?q=10.232,10.232&z=19`), but Google Maps parses the incoming data itself wrong. – Luke Vo Sep 06 '13 at 09:10
0

Updating the Google Maps app to the latest version (7.2.0) seems to fix the issue.

Litrik De Roy
  • 823
  • 5
  • 9
-2

in Xamarin.Android:

using System.Globalization;

Android.Net.Uri.Parse("http://maps.google.com/maps?daddr=" + myLatitude.ToString(CultureInfo.InvariantCulture) + "," +  myLongitude.ToString(CultureInfo.InvariantCulture)));
Moak
  • 12,596
  • 27
  • 111
  • 166
Nessitro
  • 11
  • 5