Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中将坐标传递给Google地图_Android_Google Maps_Android Studio_Android Intent - Fatal编程技术网

在android中将坐标传递给Google地图

在android中将坐标传递给Google地图,android,google-maps,android-studio,android-intent,Android,Google Maps,Android Studio,Android Intent,我已将纬度和经度值存储在firebase中,并使用搜索功能在循环视图列表中检索。我想让它在谷歌地图上显示位置时,按钮地图是点击每个项目的基础上每个文本视图纬度和经度值。如何将该值传递给“地图”活动以在google地图上显示位置标记 SearchActivity.java 公共类SearchActivity扩展AppCompatActivity实现View.OnClickListener{ EditText EditTextZone; RecyclerView recyclerView; Data

我已将纬度和经度值存储在firebase中,并使用搜索功能在循环视图列表中检索。我想让它在谷歌地图上显示位置时,按钮地图是点击每个项目的基础上每个文本视图纬度和经度值。如何将该值传递给“地图”活动以在google地图上显示位置标记

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

}

您可以将
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"));

        }
    }
}
公共类MapViewActivity扩展了活动在MapReadyCallback上的实现{
ArrayList学生关系专家;
ArrayList学生长学者;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
studentLatitudeList=getIntent().GetStringArrayListXTRA(“studentLatitudeList”);
studentLongitudeList=getIntent().getStringArrayListXTRA(“studentLongitudeList”);
MapFragment MapFragment=(MapFragment)getFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
@凌驾
已于4月1日公开作废(谷歌地图){
对于(int i=0;iSend

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

好吧,你的方法,使名单在我不好。我会建议作出一个数据模型为回收者的看法

让我们来回答您的问题,我已经看到您已经将经度和纬度传递给了MapViewActivity,所以这里是方法(只需复制下面的代码并替换语法中的任何键入错误)

}


试试看,它一定有用。

有几种方法可以在活动之间传递数据,如
共享首选项
数据库
静态类
,但最简单的方法是通过
意图
传递数据

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

因此,在活动A中

Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putFloat("latitude", latitudeValue);
intent.putFloat("longitude", longitudeValue);
startActivity(intent);
现在,要在
活动B
中接收数据

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

是否要将数据从一个活动传递到第二个活动?使用的位置searchadapter@AdityaSonel是的,我想在搜索活动中将纬度和经度传递给地图activity@JavaGuy检查我的答案。是否返回到主要活动?
    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"));
   }
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"));
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putFloat("latitude", latitudeValue);
intent.putFloat("longitude", longitudeValue);
startActivity(intent);
Intent intent = getIntent();
Float latitude = intent.getFloat("latitude");
Float longitude = intent.getFloat("longitude");