2017-06-24 10 views
-1

私はGoogleマップのアクティビティを使用しています。しかし、それは実際の場所と完全に動作します。どうやってクラッシュするのを防ぐことができますか?は、入力した場所が実際でないときにシャットダウンします。

マイコード:

public void onSearch(View view) { 
    String location = locationTS.getText().toString(); 
    if (location != null || !location.equals("")) { 
     Geocoder geocoder = new Geocoder(this); 
     List<Address> addressList=null; 
     try { 
      addressList= geocoder.getFromLocationName(location, 1); 
      mMap.clear(); 
     } 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.newLatLngZoom(latLng,80)); 
     latitudeB=latLng.latitude; 
     longitudeB=latLng.longitude; 
    } 
    else { 
     Toast.makeText(getApplicationContext(), "please fill in an available location", 
       Toast.LENGTH_LONG).show(); 

    } 
} 

答えて

0

を試してみてください、あなたのコード内の緯度と経度取得する前に

address.hasLatitude() && address.hasLongitude() 

をこのチェックを追加します。

一致するものが見つからなかったり、利用可能なバックエンドサービスがない場合は、空のリストを返します。したがって

、あなたが行うことができますあなたの問題を解決するために:

if (addressList != null && !addressList.isEmpty()) { 
    Address address=addressList.get(0); 
    LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude()); 
    mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,80)); 
    latitudeB=latLng.latitude; 
    longitudeB=latLng.longitude; 
} else { 
    Toast.makeText(getApplicationContext(), "No location found", 
      Toast.LENGTH_LONG).show(); 
} 
0

Geocoder.getFromLocationName方法のthe documentationからこの

public void onSearch(View view) { 
     String location = locationTS.getText().toString(); 
     if (location != null || !location.equals("")) { 
      Geocoder geocoder = new Geocoder(this); 
      List<Address> addressList=null; 
      try { 
       addressList= geocoder.getFromLocationName(location, 1); 
       mMap.clear(); 
      Address address=addressList.get(0); 
      // check if it has lat and long 
      if(address.hasLatitude() && address.hasLongitude()){ 
        LatLng latLng=new LatLng(address.getLatitude(),address.getLongitude()); 
      mMap.addMarker(new MarkerOptions().position(latLng).title("Marker")); 
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,80)); 
      latitudeB=latLng.latitude; 
      longitudeB=latLng.longitude; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 



     } 
     else { 
      Toast.makeText(getApplicationContext(), "please fill in an available location", 
        Toast.LENGTH_LONG).show(); 

     } 
    } 
+0

はまだクラッシュ.. –

+0

をここでログの猫を投稿してください –

関連する問題