2017-05-29 58 views
1

我試圖在谷歌地圖活動中搜索位置名稱。在onsearch方法中,address.getLatitude()和address.getLongitude不是work.its顯示紅色text.anyone請解決我的錯誤。下面我的代碼在谷歌地圖上搜索位置名稱

public void onSearch(View view) 
{ 
    EditText location_tf = (EditText)findViewById(R.id.TFaddress); 
    String location = location_tf.getText().toString(); 
    List<Address> addressList = null; 
    if(location != null || !location.equals("")) 
    { 
     Geocoder geocoder = new Geocoder(this); 
     try { 
      addressList = geocoder.getFromLocationName(location , 1); 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     Address address = addressList.get(0); 
     LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude()); 
     mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
     mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 

    } 
} 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(0,0); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 
+0

你能告訴錯誤? –

+0

錯誤意味着它只是顯示紅色文字 –

+0

通過懸停在那裏,你會得到錯誤名稱。 –

回答

0

看來,您有導入了錯誤的地址類或根本不導入。 The class which you are looking爲可使用下面的行中導入:

import android.location.Address; 

上述類包含getLatitude()getLongitude()方法。其中還包含Address類,我想,你所輸入的錯誤另一個包是以下幾點:

com.google.android.gms.identity.intents

+0

是的 –