简体   繁体   English

如何绘制从ListView项获取的两个地理位置之间的标记和路线?

[英]How to draw Marker and route between two geo points taken from ListView item?

I have task. 我有任务 I have to display messages from a specific Mobile number on a ListView in one activity. 我必须在一个活动中在ListView上显示来自特定手机号码的消息。 These Messages contains Latitude and Longitude of two Locations. 这些消息包含两个位置的纬度和经度。 When I click any of the List item it should draw markers on that two locations and have to draw route between them in another activity. 当我单击任何列表项时,它应该在两个位置上绘制标记,并且在另一个活动中必须在它们之间绘制路线。 My Message format is !+11.3326,+077.7193,08:41!+11.3326,+077.7194,08:41# 我的邮件格式为!+ 11.3326,+ 077.7193,08:41!+ 11.3326,+ 077.7194,08:41#

My MainActivity.java is 我的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;
}

My MapsActivity.java is public class MapsActivity extends FragmentActivity { 我的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"));
}

I have displayed the messages and can be able to get inputs from ListView item. 我已经显示了消息,并且能够从ListView项获取输入。 I don't know how to pass those lat1 , lng1 , lat2 , lng2 values to MapsActivity and add Markers. 我不知道如何将那些lat1lng1lat2lng2值传递给MapsActivity并添加标记。 Any solutions please. 请提供任何解决方案。

When you launch your map activity pass the latlng values in your intent. 启动地图活动时,请在意图中传递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);
    }
});

now in your on create of Maps activity get those values back. 现在,在您创建地图活动中,将这些值取回。

@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"));
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM