这就是我想要做的:用户将引入一个字符串,如果该字符串存在于我的HashMap中,则将调用gotoLocation方法,并且该映射将以该字符串的相应标记为中心。问题是我无法从HashMap中获取Latlng。这就是我到目前为止所做的:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
GoogleMap mMap;
HashMap<String, LatLng> thePlaces;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleApiClient mGoogleApiClient;
public static final String TAG = MainActivity.class.getSimpleName();
Marker myPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
assert getSupportActionBar() != null;
ActionBar actionBar = getSupportActionBar();
actionBar.setLogo(R.mipmap.ic_launcher);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
thePlaces = new HashMap<String, LatLng>();
thePlaces.put("Thirsty Monk", new LatLng(43.607044, 1.450307));
thePlaces.put("Cocolino", new LatLng(43.571505, 1.417759));
thePlaces.put("The Melting Pot", new LatLng(43.607469, 1.447162));
thePlaces.put("De Danu", new LatLng(43.600723, 1.455917));
thePlaces.put("Carciuma", new LatLng(43.604892, 1.476562));
thePlaces.put("Boca", new LatLng(43.604496, 1.474924));
thePlaces.put("Bar Acasa", new LatLng(43.604781, 1.474502));
//AutoCompleteTextView t1 = (AutoCompleteTextView)findViewById(R.id.editText1);
setThePlaces(thePlaces.keySet().toArray(new String[1000]));
}
private void setThePlaces (String searchString[]){
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, searchString);
AutoCompleteTextView t1 = (AutoCompleteTextView)findViewById(R.id.editText1);
assert t1 != null;
t1.setThreshold(2);
t1.setAdapter(adapter);
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.location:
Toast.makeText(getApplicationContext(), "Location selected", Toast.LENGTH_LONG).show();
return true;
case R.id.map_type:
Toast.makeText(getApplicationContext(), "Map Type selected", Toast.LENGTH_LONG).show();
return true;
case R.id.share_app:
Toast.makeText(getApplicationContext(), "Share the APP", Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng cityView = new LatLng(43.604346, 1.443760);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(cityView, 12));
MarkerOptions beerMarker = new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.beer_marker));
// The Thirsty Monk
LatLng thirstyMonk = new LatLng(43.607044, 1.450307);
mMap.addMarker(beerMarker.position(thirstyMonk).title("The Thirsty Monk").snippet("Text example!" + "\n text example second line"));
// The Cocolino
LatLng cocolino = new LatLng(43.571505, 1.417759);
mMap.addMarker(beerMarker.position(cocolino).title("Cocolino").snippet("Lets do the twist"));
//The Melting Pot
LatLng meltingPot = new LatLng(43.607469, 1.447162);
mMap.addMarker(beerMarker.position(meltingPot).title("The Melting Pot"));
//De Danu
LatLng deDanu = new LatLng(43.600723, 1.455917);
mMap.addMarker(beerMarker.position(deDanu).title("De Danu"));
//Carciuma
LatLng carciuma = new LatLng(43.604892, 1.476562);
mMap.addMarker(beerMarker.position(carciuma).title("Carciuma"));
//Boca
LatLng boca = new LatLng(43.604496, 1.474924);
mMap.addMarker(beerMarker.position(boca).title("Boca"));
//Bar Acasa
LatLng barAcasa = new LatLng(43.604781, 1.474502);
mMap.addMarker(beerMarker.position(barAcasa).title("Bar Acasa"));
}
public void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
public void locateFromString(View view) {
hideSoftKeyboard(view);
TextView tv = (TextView) findViewById(R.id.editText1);
String searchString = tv.getText().toString();
if (thePlaces.containsKey(searchString)){
Toast.makeText(this,"The place exists in the list", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"The place DOESN'T EXIST in the list", Toast.LENGTH_SHORT).show();
}
}
提前感谢您的帮助!
答案 0 :(得分:0)
试试这个
String placeKey "APlaceKey";
LatLng selectedValue = null;
if (thePlaces.get(placeKey) != null) {
selectedValue = thePlaces.get(placeKey);
}
if (selectedValue != null) {
// do your code here with the selected LatLng
}