2015-05-13 54 views
0

我有任務。我必須在一個活動中顯示來自ListView上的特定手機號碼的消息。這些消息包含兩個位置的經度和緯度。當我點擊任何List項目時,它應該在這兩個位置繪製標記,並且必須在另一個活動中繪製它們之間的路線。 我的消息格式+ 11.3326,+ 077.7193,08:!41 + 11.3326,+ 077.7194,08:41#如何在兩個從ListView項獲取的地理點之間繪製標記和路線?

我MainActivity.java是

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final ListView listView = (ListView) findViewById(R.id.listView); 
    List<String> msgList=getSMS(); 
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,msgList); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      String str=(String)parent.getItemAtPosition(position); 
      String lat1=str.substring(2,8); 
      String lng1=str.substring(11,18); 
      String lat2=str.substring(27,33); 
      String lng2=str.substring(36,43); 
      Toast.makeText(getApplicationContext(),lat1+""+lng1+""+lat2+""+lng2,Toast.LENGTH_SHORT).show(); 
      Intent intent=new Intent(MainActivity.this,MapsActivity.class); 
      startActivity(intent); 
     } 
    }); 
} 

public List<String> getSMS(){ 
    List<String>sms=new ArrayList<String>(); 
    final String SMS_URI_INBOX="content://sms/inbox"; 
    try { 
     Uri uri=Uri.parse(SMS_URI_INBOX); 
     String[] projection=new String[]{"_id","address","person","body","date","type"}; 
     Cursor cursor=getContentResolver().query(uri,projection,"address='+918870346164'",null,null); 
     if (cursor.moveToFirst()){ 
      int index_Address = cursor.getColumnIndex("address"); 
      int index_Person = cursor.getColumnIndex("person"); 
      int index_Body = cursor.getColumnIndex("body"); 
      int index_Date = cursor.getColumnIndex("date"); 
      int index_Type = cursor.getColumnIndex("type"); 
      do { 
       String strAddress = cursor.getString(index_Address); 
       int intPerson = cursor.getInt(index_Person); 
       String strBody = cursor.getString(index_Body); 
       String longDate = cursor.getString(index_Date); 
       int intType = cursor.getInt(index_Type); 

       sms.add("\n"+strBody+"\n"); 
      }while (cursor.moveToNext()); 
      if (!cursor.isClosed()){ 
       cursor.close(); 
       cursor=null; 
      } 
     } 
    }catch (SQLiteException e){ 
     Log.d("SQLiteException",e.getMessage()); 
    } 
    return sms; 
} 

我MapsActivity.java是 公共類MapsActivity擴展FragmentActivity {

GoogleMap mMap; // Might be null if Google Play services APK is not available. 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    setUpMapIfNeeded(); 
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    mMap.getUiSettings().setZoomGesturesEnabled(true); 
    mMap.getUiSettings().setMyLocationButtonEnabled(true); 
    addMarker(); 

} 

private void addMarker() { 
    final LatLng latLng=new LatLng(lat1,lng1); 
    mMap.addMarker(new MarkerOptions().position(latLng).title("My Marker")); 
} 

我已經顯示的信息,可以是能夠從ListView項目得到的投入。我不知道如何通過那些lat1,lng1,lat2,lng2值到MapsActivity並添加標記。請任何解決方案。

回答

0

當您啓動地圖活動時,請在您的意圖中傳遞latlng值。

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String str=(String)parent.getItemAtPosition(position); 
     Intent intent=new Intent(MainActivity.this,MapsActivity.class); 
     intent.putExtra("locationValues",str); 
     startActivity(intent); 
    } 
}); 

現在在您創建Maps活動時將這些值返回。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    setUpMapIfNeeded(); 
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    mMap.getUiSettings().setZoomControlsEnabled(true); 
    mMap.getUiSettings().setZoomGesturesEnabled(true); 
    mMap.getUiSettings().setMyLocationButtonEnabled(true); 
    String locationString = this.getIntent().getStringExtra("locationValues",""); 
    if(!TextUtils.isEmpty(locationString)) 
    { 
     String lat1=str.substring(2,8); 
     String lng1=str.substring(11,18); 
     String lat2=str.substring(27,33); 
     String lng2=str.substring(36,43); 
     addMarker(lat1,lng1,lat2,lng2); 
    } 

}

private void addMarker(String lat1,String lng1,String lat2,String lng2) { 
    //you can use those value here then. 
    final LatLng latLng=new LatLng(lat1,lng1); 
    mMap.addMarker(new MarkerOptions().position(latLng).title("My  Marker")); 
} 
相關問題