1

Here is my code. Please tell me solution. I tried all possible solutions that stackOverFlow describes previously. I want to get Latitude and Longitude values from an user entered Address.

public class MainActivity extends ActionBarActivity {

    private GoogleMap googleMap;
    private double latitude;
    private double longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        latitude = 32.483377;
        longitude = 74.53143249999994;
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Phone will Mute in this Region");
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        googleMap.setMyLocationEnabled(true); // false to disable
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.addMarker(marker);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // Instantiates a new CircleOptions object and defines the center and radius
        CircleOptions circleOptions = new CircleOptions()
                .center(new LatLng(32.483377, 74.53143249999994))
                .radius(500);


        Circle circle = googleMap.addCircle(circleOptions);
        circle.setFillColor(Color.argb(100, 243, 85, 133));
        circle.setStrokeWidth(0);
        CameraPosition cameraPosition = new CameraPosition.Builder().target(
                new LatLng(32.483377, 74.53143249999994)).zoom(12).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }

    public void Search_Adderess(View view) throws IOException {
        Hide_Keyboard(view);
        EditText address = (EditText) findViewById(R.id.ad);

        String location = address.getText().toString();
        Geocoder geocoder = new Geocoder(this);  

        try {
            List<Address> add = geocoder.getFromLocationName(location, 1);
            for (int i= 0; i<100; i++) {
                add = geocoder.getFromLocationName(location, 1);   
            }
                if(geocoder.isPresent())   // returning false 
                    Toast.makeText(this,"Geocorder is implemented", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(this,"Geocorder could not implemented", Toast.LENGTH_LONG).show();

                if (add.size()>0) {
                    Address new_address = add.get(0);
                    String locality = new_address.getLocality();
                    Toast.makeText(this, locality, Toast.LENGTH_LONG).show();
                    double search_latitude = new_address.getLatitude();
                    double search_longitude = new_address.getLongitude();
                    get_location_address(search_latitude, search_longitude, 15);
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
    private void Hide_Keyboard(View view){

        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    public void get_location_address(double lati, double longi, float zoom){
        LatLng latLng = new LatLng(lati,longi);
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
        googleMap.moveCamera(update);

    }
}
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28
  • Returns true if the Geocoder methods getFromLocation and getFromLocationName are implemented. Lack of network connectivity may still cause these methods to return null or empty lists. http://developer.android.com/reference/android/location/Geocoder.html#isPresent() – Manish Apr 22 '15 at 08:44
  • Thank you for reply. how to fix this? Manish – Sunny Muzammil Apr 22 '15 at 08:47
  • Actually the Geocoder need a Service running in the background by the framework. From the documentation: The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists. Do you have latest version of play service installed on your device? Just a guess I am not sure how you can actually fix it. – Manish Apr 22 '15 at 09:05
  • Yes, I have Google play services updated version installed in my device. – Sunny Muzammil Apr 22 '15 at 09:36
  • can anybody test this code and come back to here to let me know its working fine or not? Thank you very much for your help – Sunny Muzammil Apr 22 '15 at 09:54

1 Answers1

-1

Use "AsyncTask" to fetch coordinates from server. For example "getFromLocationName()" should be called using AsyncTask. UI thread (main activity) does not allow the tasks which take too much time thats why the method returns empty list