Implement map search function on custom Application like in native google maps-Collection of common programming errors
-
By Name og Location
public void searchPlace() { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Search Location"); alert.setMessage("Enter Location Name: "); // Set an EditText view to get user input final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText().toString(); // Do something with value! Log.d("value", value); Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault()); try { List addresses = geoCoder.getFromLocationName( value, 5); String add = ""; if (addresses.size() > 0) { p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), (int) (addresses.get(0).getLongitude() * 1E6)); mc.animateTo(p); // create mapController object like `MapController mc = mapView.getController();` mapView.invalidate(); } } catch (IOException e) { e.printStackTrace(); } } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert.show(); }
-
By LAtLang.
public void byLatLang() { LayoutInflater factory = LayoutInflater.from(this); final View textEntryView = factory.inflate(R.layout.latlong, null); AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Search Location"); alert.setMessage("Enter Lattitude and Longitude: "); alert.setView(textEntryView); // Set an EditText view to get user input AlertDialog latLongPrompt = alert.create(); final EditText lat = (EditText) textEntryView.findViewById(R.id.lat); final EditText longi = (EditText) textEntryView.findViewById(R.id.longi); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "clicked ok ", Toast.LENGTH_SHORT).show(); Double value1 = Double.parseDouble(lat.getText().toString()); Double value2 = Double.parseDouble(longi.getText().toString()); // Do something with value! //Log.d("value1", value1); //Log.d("value2", value2); p = new GeoPoint( (int) (value1 * 1E6), (int) (value2 * 1E6)); mc.animateTo(p); mc.setZoom(17); mapView.invalidate(); } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert.show(); }