Add a search button to maps

Wednesday, November 4, 2015

  1. This is normal text.
    Hi,
    I am new to android and I am trying to add a search bar to android maps.
    The map is added as one of the fragment to the project.
    The error seems be introduced by "this".
    Any help is appreciated.
    Thanks in advance.


  2. So is this, but now follows a code block:




public class Maps extends Fragment {



//Initial code public void 

onSearch(View view) {
EditText location_tf = (EditText) getView().findViewById(R.id.TFaddress);
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());
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));


}



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">


<EditText android:layout_width="287dp" android:layout_height="wrap_content" android:id="@+id/TFaddress"/>

<Button style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Search"
android:id="@+id/button" android:layout_gravity="right" android:onClick="onSearch" />



<com.google.android.gms.maps.MapView android:id="@+id/map"
android:layout_width="match_parent" android:layout_height="match_parent"/>
</LinearLayout>
Error:(83, 46) error: incompatible types: MAPS cannot be converted to Context
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output.
Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.

0 comments:

Post a Comment