0

I have this problem.

I want to show a specific location on maps in android. Like if user has entered India in Edit text, then how can i show INDIA on Map. I basically want to know how to get the latitude and longitude of any location.

Please help

My Code:

public class Map extends Activity{
private GoogleMap map;
private LatLng myLoc;

@SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    Intent i=getIntent();
    String loc=i.getStringExtra("loc");
    map  = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());  

    try {
        List<Address> addresses = geoCoder.getFromLocationName(
            loc, 5);

        if (addresses.size() > 0) {
            myLoc = new LatLng(
                    (int) (addresses.get(0).getLatitude() * 1E6), 
                    (int) (addresses.get(0).getLongitude() * 1E6));
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(myLoc, 14);
            map.animateCamera(update);
        }    
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

It is not Working. Please Help

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94

3 Answers3

1

It seems that Geocoder functions are blocked and use network. Because of onCreate is running on main thread, you can get this error(in older versions of android). Thus, you have to create AsyncTask and move geocoder's function to AsyncTask.doInBackground().

And after, in AsyncTask.onPostExecute() you should perfom the other operations with google map. (It will be on main thread in this case, how it must be)

Community
  • 1
  • 1
matreshkin
  • 2,199
  • 18
  • 28
0

Use

map.setMyLocationEnabled(true);

A blue circle will be appeared on map. It will show the current location.

Nishant Shah
  • 3,442
  • 5
  • 25
  • 34
0

When you select India from edittext call this function:

public void showSelectedCountry(String countryName) {

    if (countryName.equals("India")) {

        LatLngBounds boundsIndia = new LatLngBounds(new LatLng(23.63936, 68.14712), new LatLng(28.20453, 97.34466));
        int padding = 0; // offset from edges of the map in pixels
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundsIndia, padding);
        mMap.animateCamera(cameraUpdate);

    }

}
Yogesh Nikam Patil
  • 1,192
  • 13
  • 18