1

Here is my code.. I want to find just a name of city from latitude and longitude.. Now i get this :

"Address[addressLines=[0:"Mustamäe tee 145",1:"12918 Tallinn",2:"Estonia"],feature=145,admin=Harju County,sub-admin=null,locality=Tallinn,thoroughfare=Mustamäe tee,postalCode=12918,countryCode=Estonia,countryName=null,hasLatitude=true,latitude=59.4123264,hasLongitude=true,longitude=24.6903851,phone=null,url=null,extras=null]"

I tried to convert it to string and split it.. i made a String array but when i try to assign "cityName" to String array "name" I get this error - "Type mismatch: cannot convert from String[] to String"

Can you tell me please where am I wrong?

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GPSHelper {
static double latitude;
static double longitude;
static String[] name = new String[5];

public static void getCity(LocationManager lm, Context appContext) {

    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    longitude = location.getLongitude();
    latitude = location.getLatitude();
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
        }

        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }
    };

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    Geocoder userLocation = new Geocoder(appContext, Locale.getDefault());   
    List<Address> cityName;
    try {
        cityName = userLocation.getFromLocation(latitude, longitude, 1);
        if(cityName != null && !cityName.isEmpty()) {
            for(int i=0; i<5; i++){

            name[i] = cityName.get(0).toString().split("");

            }
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

}

7 Answers 7

3

All the answers here are fine. But what I wonder is why would you use a split on empty character?

Do you know it returns array of single characters (and nothing at start)?

Checkout the code:

String str ="dasd";
String[] strr = str.split("");
for(int i=0; i < strr.length;i++)
    System.out.println("i="+i+";str="+strr[i]);

Output:

i=0;str=
i=1;str=d
i=2;str=a
i=3;str=s
i=4;str=d

I would suggest that since cityName.get(0) returns Address object have a method there which will return you its name, like below.

name[i] = cityName.get(0).getName();

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm so stupid:).. Thanks.... But helped this : name=cityName.get(0).getLocality();
@Sergio pleasure to help. Enjoy Java
2
name[i] = cityName.get(0).toString().split("");

name[i] is of type String while cityName.get(0).toString().split("") is of type String[]

Comments

1
name[i] = cityName.get(0).toString().split("");

here name[i] will hold only string, but split("") method needs String Array in left side.

Is that for loop really needed?? Waht about removing the for loop and code it like

   if(cityName != null && !cityName.isEmpty()) {         

        name[] = cityName.get(0).toString().split("");

    }

Comments

0

The following line is wrong:

name[i] = cityName.get(0).toString().split("");

(as the exception message probably indicates). Indeed, you're trying to initialize name[i], which is a String, with the result of the split method, which returns an array of Strings.

Comments

0

I do not have any experience with Android, so I don't know what those methods should return, but String.split will return a String array. And name[i] is only a String. So you are trying to assign a String array to a simple String.

Comments

0

the Split method returns an array of String ; so when you do this :

name[i] = cityName.get(0).toString().split("");

it will return an array of Strings which will contain all of letters of your String ,

Comments

0
cityName.get(0).toString().split("");

That returns a String[] as result, but name[i] should be a String and not a String[]

Why do you split?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.