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);
}
}