2016-04-06 226 views
0

这就是我想要做的:用户将引入一个字符串,如果该字符串存在于我的HashMap中,那么gotoLocation方法将被调用,并且地图将集中在该字符串的相应标记上。问题是我无法从HashMap中获取Latlng。这是我到目前为止:从HashMap获取坐标

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(); 
     } 


    } 

在此先感谢您的帮助!

+3

欢迎堆栈溢出。你已经发布了大量的代码,其中大部分与这个问题无关......但是你没有包含重要的信息,比如你实际观察到的内容。例如,当你调试代码时'searchString'的价值是什么? –

+1

'LatLng loc = thePlaces.get(searchString)',不是?而'gotoLocation()'方法的condider重构使它接受'LatLng'而不是单独的'lat'和'lon',所以你不需要创建一个新的'latLng'。 –

+0

谢谢Sasha,它工作:)我已经将您的代码添加到locateFromString方法,并且我完全删除了gotoLocation方法,因为我现在没有看到它相关,因为我可以在if语句中创建latLng。你的观点有什么问题吗?我如何将你的答案标记为正确答案? –

回答

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 
}