Reputation: 382
How to find current location with a marker. This code it finds the location but how to show the marker in current location?
public void onSearch (View view)
{
ibtn = (ImageButton)findViewById(R.id.imageSearchButton);
EditText location_tf = (EditText) findViewById(R.id.editTextSearch);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if(location != null || !location.equals(""))
{
Geocoder geocoder = new Geocoder(this);
try{
addressList = geocoder.getFromLocationName(location, 1);
}catch (IOException e)
{
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Accidents Here"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
Upvotes: 2
Views: 11337
Reputation: 898
try this,
write onCreate() in your main activity class.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gooogle_map);
TextView tvAddress = (TextView) findViewById(R.id.textView1);
GoogleMap googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
Location l = null;
for (int i = 0; i < providers.size(); i++) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) {
latitude = l.getLatitude();
longitude = l.getLongitude();
strAdd = getCompleteAddressString(latitude, longitude);
tvAddress.setText("Complete Address : " + strAdd);
break;
}
}
if (googleMap != null) {
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps").snippet("Discription");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
// Moving Camera to a Location with animation
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
googleMap.addMarker(marker);
}
}
and this is your getCompleteAddressString().
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<android.location.Address> addresses = geocoder
.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
android.location.Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress
.append(returnedAddress.getAddressLine(i)).append(
"\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address",
"" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}
Upvotes: 2