Jayce
Jayce

Reputation: 842

android google maps lat/long

I am writing an app that will convert an address input into lat/long coordinates and then display them on a map for the user. Everything is working great, except my coordinates are coming out like .. 35429885,-9745332 .. You see there is no period to define them. I need them to come out looking like this .. 35.429885,-97.45332 in order for google maps api to display the marker. Is there a way to input the period, or am I doing something wrong with my code? Below is the code I use to convert the address to lag/long. Thanks in advance.

geocoder = new Geocoder(this);

EditText locale = (EditText) findViewById(R.id.e_addy_text);  

String locationName = locale.getText().toString();

            try
            {
                List<Address> addressList = geocoder.getFromLocationName(  
                    locationName, 5); 

                if (addressList != null && addressList.size() > 0) 
                {  
                    lat = (int) (addressList.get(0).getLatitude() * 1e6);  
                    lng = (int) (addressList.get(0).getLongitude() * 1e6); 

                    // CONVERT TO STRING
                    e_lat = Integer.toString(lat);
                    e_lng = Integer.toString(lng);

                    // input into my database.
                }
                else
                {
                    // toast saying invalid address
                }
            } 
            catch (Exception e) 
            {  
                e.printStackTrace();  
            }

Upvotes: 3

Views: 181

Answers (2)

DjP
DjP

Reputation: 4587

You have stored the lat long in integer which you have to store in double

Upvotes: 0

Nambi
Nambi

Reputation: 12042

getLatitude() and getLongitude() returns the double value which has the decimal representation. In this you are casting the double to int.

Just store the value in double variable and then convert to the String

double lat=addressList.get(0).getLatitude();

double lon=addressList.get(0).getLongitude();

convert to String

 e_lat=  String.valueOf(lat);
 e_lng  String.valueOf(lon);

Upvotes: 1

Related Questions