【问题标题】:Passing coordinates to Google Maps in android在android中将坐标传递给谷歌地图
【发布时间】:2018-06-11 20:52:24
【问题描述】:

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

SearchActivity.java

公共类 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));
    }

}

}

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

【问题讨论】:

  • 您想将数据从一个活动传递到第二个活动吗?
  • 你在哪里使用了 searchadapter
  • @Aditya Sonel 是的,我想将搜索活动中的经纬度传递给地图活动
  • @JavaGuy 检查我的答案。

标签: android google-maps android-studio android-intent


【解决方案1】:

发送

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

【讨论】:

    【解决方案2】:

    嗯,你列出清单的方法对我不利。我会建议为回收者视图制作一个数据模型。

    让我们来回答您的问题,我已经看到您已将经度和纬度传递给 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"));
    

    }

    试一试,一定可以的。

    【讨论】:

    • 它返回主要活动?
    【解决方案3】:

    有几种方法可以在 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");
    

    【讨论】:

      【解决方案4】:

      您可以将studentLatitudeList 作为列表传递

      传递数据

      Intent intent = getIntent();  
      intent.putStringArrayListExtra("studentLatitudeList", studentLatitudeList);
      intent.putStringArrayListExtra("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"));
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-05
        • 1970-01-01
        • 2017-01-09
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多