I'm developing an Android application that works with the Maps API, and in it I have a searchView that needs to do the search of the location typed and display on the map. But I do not know how to insert a script (I gave a searched and mostly the codes that can do such a search, do it with JavaScript code). Here is my code:
Map screen XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/re/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.vitor.agrvaimaps.ActPrincipal"
tools:showIn="@layout/app_bar_act_principal">
<FrameLayout
android:id="@+id/container"
android:layout_width="382dp"
android:layout_height="510dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="0dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</FrameLayout>
<SearchView
android:id="@+id/searchView"
android:iconifiedByDefault="false"
android:layout_width="300dp"
android:layout_height="39dp"
android:queryHint="Pesquise um Local"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="0dp" />
</RelativeLayout>
The Activity Code:
public class ProviderV2 extends SupportMapFragment implements OnMapReadyCallback,
GoogleMap.OnMapClickListener, LocationListener {
private static final String TAG = "ProviderV2";
private GoogleMap mMap;
private Marker marker;
private SearchView busca;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
@Override
public void onResume() {
super.onResume();
//Ativa GPS
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, this);
}
@Override
public void onPause() {
super.onPause();
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
try {
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
mMap = googleMap;
mMap.setOnMapClickListener(this);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMyLocationEnabled(true);
}catch (SecurityException ex){
Log.e(TAG,"Erro", ex);
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
if(marker != null){
marker.remove();
}
customAddMarker( new LatLng(latLng.latitude, latLng.longitude), "Novo Local", "Clique aqui para adicionar aos favoritos!");
}
});
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) { // MARKER CLICK LISTENER
return false;
}
});
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
//Marcador
}
}); // CLICAR NO DROPMENU DO MARKER
}
public void setOnSearchClickListener(View.OnClickListener listener){ //METODO QUE ATIVA O LISTENER DO BOTAO DE PESQUISA
//Aqui é onde preciso inserir o script para busca do local
}
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText( getContext(),"Coordenadas: " + latLng.toString(), Toast.LENGTH_SHORT).show();
//Não funcional
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(getContext(), "Você se moveu", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText( getContext(),"Status Alterado. ", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText( getContext(),"Provider Habilitado.", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText( getContext(),"Provider Desabilitado.", Toast.LENGTH_SHORT).show();
}
public void customAddMarker(LatLng latLng, String title, String snippet){
MarkerOptions options = new MarkerOptions();
options.position(latLng).title(title).snippet(snippet).draggable(true);
marker = mMap.addMarker(options);
}
}