将坐标传递到Android中的Google地图

问题描述 投票:-1回答:4

我已将纬度和经度值存储在firebase中,并使用搜索功能在回收视图列表中检索。我希望它根据每个文本视图的纬度和经度值在每个项目上单击按钮地图时显示谷歌地图中的位置。如何将值传递给maps活动以在Google地图上显示位置标记。

search activity.Java

public class SearchActivity扩展AppCompatActivity实现View.OnClickListener {

EditText EditTextZone;
RecyclerView recyclerView;
DatabaseReference databaseReference;
FirebaseUser firebaseUser;
ArrayList<String> studentNameList;
ArrayList<String> studentMatrikList;
ArrayList<String> studentPhoneList;
ArrayList<String> studentAddressList;
ArrayList<String> studentLatitudeList;
ArrayList<String> studentLongitudeList;
SearchAdapter searchAdapter;
Button buttonCancel;
Button buttonGo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    EditTextZone = (EditText) findViewById(R.id.EditTextZone);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    buttonCancel = (Button) findViewById(R.id.buttonCancel);
    buttonGo = (Button) findViewById(R.id.buttonGo);

    buttonCancel.setOnClickListener(this);
    buttonGo.setOnClickListener(this);

    databaseReference = FirebaseDatabase.getInstance().getReference();
    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

    studentNameList = new ArrayList<>();
    studentMatrikList = new ArrayList<>();
    studentPhoneList = new ArrayList<>();
    studentAddressList = new ArrayList<>();
    studentLatitudeList = new ArrayList<>();
    studentLongitudeList = new ArrayList<>();

    EditTextZone.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

            if(!s.toString().isEmpty()){

                setAdapter(s.toString());

            } else {
                studentNameList.clear();
                studentMatrikList.clear();
                studentPhoneList.clear();
                studentAddressList.clear();
                studentLongitudeList.clear();
                studentLatitudeList.clear();
                recyclerView.removeAllViews();
            }

        }
    });


}

private void setAdapter(final String searchedString) {

    databaseReference.child("student").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            studentNameList.clear();
            studentMatrikList.clear();
            studentPhoneList.clear();
            studentAddressList.clear();
            studentLongitudeList.clear();
            studentLatitudeList.clear();
            recyclerView.removeAllViews();

            int counter = 0;

            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                String id = snapshot.getKey();
                String studentName = snapshot.child("studentName").getValue(String.class);
                String studentMatrik = snapshot.child("studentMatrik").getValue(String.class);
                String studentPhone = snapshot.child("studentPhone").getValue(String.class);
                String studentAddress = snapshot.child("studentAddress").getValue(String.class);
                String studentLongitude = snapshot.child("studentLongitude").getValue(String.class);
                String studentLatitude = snapshot.child("studentLatitude").getValue(String.class);


                if (studentAddress.toLowerCase().contains(searchedString.toLowerCase())) {

                    studentNameList.add(studentName);
                    studentMatrikList.add(studentMatrik);
                    studentPhoneList.add(studentPhone);
                    studentAddressList.add(studentAddress);
                    studentLatitudeList.add(studentLatitude);
                    studentLongitudeList.add(studentLongitude)
                    counter++;


                } else if (studentName.toLowerCase().contains(searchedString.toLowerCase())) {
                    studentNameList.add(studentName);
                    studentMatrikList.add(studentMatrik);
                    studentPhoneList.add(studentPhone);
                    studentAddressList.add(studentAddress);
                    studentLatitudeList.add(studentLatitude);
                    studentLongitudeList.add(studentLongitude);
                    counter++;
                }

                if (counter == 15)
                    break;
            }

            searchAdapter = new SearchAdapter(SearchActivity.this, studentNameList, studentMatrikList, studentPhoneList, studentAddressList, studentLongitudeList, studentLatitudeList);
            recyclerView.setAdapter(searchAdapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}


@Override
public void onClick(View v) {

    if (v == buttonCancel)
    {
        finish();
        startActivity(new Intent(this, UserAreaActivity.class));
    }

    if (v == buttonGo)
    {
        startActivity(new Intent(this, MapsActivity.class));
    }

}

}

search adapter.Java

公共类SearchAdapter扩展RecyclerView.Adapter {

Context context;
ArrayList<String> studentNameList;
ArrayList<String> studentMatrikList;
ArrayList<String> studentPhoneList;
ArrayList<String> studentAddressList;
ArrayList<String> studentLatitudeList;
ArrayList<String> studentLongitudeList;

class SearchViewHolder extends RecyclerView.ViewHolder{

    TextView studentName, studentMatrik, studentPhone, studentAddress, studentLongitude, studentLatitude;
    CheckBox checkBox;
    Button buttonMap;


    public SearchViewHolder(View itemView) {
        super(itemView);
        studentName = (TextView) itemView.findViewById(R.id.studentName);
        studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
        studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
        studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
        studentLongitude = (TextView) itemView.findViewById(R.id.studentLongitude);
        studentLatitude = (TextView) itemView.findViewById(R.id.studentLatitude);
        checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
        buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(SearchAdapter.this.context,
                            "Selected student is " + studentName.getText(),Toast.LENGTH_LONG).show();
                } else {

                }
            }
        });
    }

}

public SearchAdapter(Context context, ArrayList<String> studentNameList, ArrayList<String> studentMatrikList, ArrayList<String> studentPhoneList, ArrayList<String> studentAddressList, ArrayList<String> studentLongitudeList, ArrayList<String> studentLatitudeList) {
    this.context = context;
    this.studentNameList = studentNameList;
    this.studentMatrikList = studentMatrikList;
    this.studentPhoneList = studentPhoneList;
    this.studentAddressList = studentAddressList;
    this.studentLongitudeList = studentLongitudeList;
    this.studentLatitudeList = studentLatitudeList;

}

@Override
public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
    return new SearchAdapter.SearchViewHolder(view);
}

@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
    holder.studentName.setText(studentNameList.get(position));
    holder.studentMatrik.setText(studentMatrikList.get(position));
    holder.studentPhone.setText(studentPhoneList.get(position));
    holder.studentAddress.setText(studentAddressList.get(position));
    holder.studentLatitude.setText(studentLatitudeList.get(position));
    holder.studentLongitude.setText(studentLongitudeList.get(position));
    holder.buttonMap.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context,MapViewActivity.class);
            intent.putExtra("Latitude", studentLatitudeList);
            intent.putExtra("Longitude", studentLongitudeList);
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {

    return studentNameList.size();
}

地图活动

public class MapViewActivity extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    Bundle bundle = getIntent().getExtras();
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Marker"));
}
android google-maps android-studio android-intent
4个回答
0
投票

发送

        Intent intent = new Intent(context,MapViewActivity.class);
        intent.putExtra("Latitude", studentLatitudeList);
        intent.putExtra("Longitude", studentLongitudeList);
        context.startActivity(intent);

收到MapActivity

    String Latitude = getIntent().getStringExtra("Latitude");
    String Longitude = getIntent().getStringExtra("Longitude");
    double lat=Double.parseDouble(Latitude);
    double lng=Double.parseDouble(Longitude);


   @Override
   public void onMapReady(GoogleMap map) {
   map.addMarker(new MarkerOptions()
        .position(new LatLng(lat, lng))
        .title("Marker"));
   }

0
投票

那么你的列表对我来说不好的做法。我建议为Recycler视图创建一个数据模型。

让我们来看看你的问题,我已经看到你已经将经度和纬度传递给MapViewActivity,所以这里是要走的路(只需复制以下代码并替换语法中的任何输入错误)

public class MapViewActivity extends Activity implements OnMapReadyCallback {   
private double latitude,longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
     Bundle extras = getIntent().getExtras();
if (extras != null) {
    latitude = Double.parseDouble(extras.getString("Latitude"));
    longitude = Double.parseDouble(extras.getString("Longitude"));
    //The key argument here must match that used in the other activity
}
    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .title("Marker"));

}

尝试一下,它必须工作。


0
投票

有几种方法可以在shared-preferencesdatabasestatic classes等活动之间传递数据,但最简单的方法是通过Intent传递数据。

让两个活动Activity AActivity B。现在,我们必须将数据从Activity A传递到Activity B

所以,在Activity A

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putFloat("latitude", latitudeValue);
intent.putFloat("longitude", longitudeValue);
startActivity(intent);

现在,要在Activity B接收数据,

Intent intent = getIntent();
Float latitude = intent.getFloat("latitude");
Float longitude = intent.getFloat("longitude");

0
投票

你可以通过studentLatitudeList列表

传递数据

Intent intent = getIntent();  
intent.putStringArrayListExtra("studentLatitudeList", studentLatitudeList);
intent.putStringArrayListExtra("studentLongitudeList", studentLongitudeList);

studentLongitudeList检索数据

public class MapViewActivity extends Activity implements OnMapReadyCallback {   


    ArrayList<String> studentLatitudeList;
    ArrayList<String> studentLongitudeList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        studentLatitudeList= getIntent().getStringArrayListExtra("studentLatitudeList");
        studentLongitudeList= getIntent().getStringArrayListExtra("studentLongitudeList");

        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        for(int i=0;i<studentLatitudeList.size();i++)
        {
        double latitude = Double.parseDouble(studentLatitudeList.get(i));
        double longitude = Double.parseDouble(studentLongitudeList.get(i));
         map.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title("Marker"));

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.