2016-04-06 311 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 
}