从android中的latlng对象获取经度和纬度

时间:2013-03-07 06:50:55

标签: android google-maps

我想在Googlemap v2上获取经度和经度..我已经使用代码为mapragment对象设置了onclick侦听器

 map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng latln) {
        // TODO Auto-generated method stub


        String s=latln.toString();

        Log.w("dsfdsf",""+s);

    }
});

它提供lat / lng格式的纬度和经度坐标:(xx.xxxxxx,yy.yyyyy) 我需要精确的方法来获取纬度和经度数据..我不想使用split解析数据并获取坐标..

5 个答案:

答案 0 :(得分:31)

这可以在Google Maps API reference

中轻松找到
  

公共最终类LatLng

     

公共最终双纬度

     

纬度,以度为单位。该值在[-90,90]范围内。

     

公共最终双重经度

     

经度,以度为单位。该值的范围为[-180,180)。

因此,在您的示例中,您应该致电:

double lat = latlng.latitude;
double lng = latlng.longitude;

答案 1 :(得分:0)

var res = results1[key][key1].toString().split("(");
console.log(" latlong::"+res);
var res2= res[1].split(",");
console.log(" latlong::"+res2);
latlongFrom[0]=res2[0];
console.log(" latlong::"+latlongFrom[0]);
var res3=res2[1].split(")");
console.log(" latlong::"+res3[0]);
latlongFrom[1]=res3[0];
console.log(" latlong::"+latlongFrom[1]);

这就是我所做的。 res包含(lat,long)的值。

答案 2 :(得分:-1)

要获得纬度和经度,您必须声明2个双变量。

map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng latln) {
        double latitude, longitude;
        latitude(latln.latitude);
        longitude(latln.longitude);

    }
});

答案 3 :(得分:-1)

如需更新版本,请使用:

    private List<Painel> paineis = new ArrayList<>();
    private List<Inversor> inversores = new ArrayList<>();

    FirebaseFirestore paineisBD = FirebaseFirestore.getInstance();
    FirebaseFirestore inversoresBD = FirebaseFirestore.getInstance();

@Override
    public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);

        LoadData();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dim_resultado_card_fragment, container, false);


        Calculate();


        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true); //para o tamanho do recycleview não mudar

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                Adapter_Produtos adapter = (Adapter_Produtos) recyclerView.getAdapter();

                if (produtosList.size() == linearLayoutManager.findLastCompletelyVisibleItemPosition() + 1) {
                    //List<Produtos_Class> listAux = ((MainActivity) getActivity().getSetCarList(10));
                    List<Produtos_Class> listAux = new ArrayList<>();
                    for (int i = 0; i < listAux.size(); i++) {
                        adapter.addListItem(listAux.get(i), produtosList.size());
                    }
                }
            }
        });


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); //Deixar scroolView padrão
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new Adapter_Produtos(produtosList);
        adapter.setRecyclerViewOnClick(this);
        Collections.sort(produtosList, Produtos_Class.menor_valor);
        recyclerView.setAdapter(adapter);

        return view;
    }

    private void LoadData() {

        paineisBD.collection("paineis").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        Log.d("PAINEIS", "ID: " + document.getId() + " => " + document.getData());
                        paineis.add(new Painel((document.getId()),
                                document.getBoolean("disponivel"),
                                document.getDouble("ef"),
                                document.getString("empresa"),
                                document.getString("marca"),
                                document.getDouble("larg"),
                                document.getDouble("alt"),
                                document.getString("link"),
                                document.getDouble("pot"),
                                document.getDouble("valor")));
                    }
                } else {
                    Log.d("PAINEIS", "Error getting documents", task.getException());
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getContext(), "Error Painel" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        inversoresBD.collection("inversores").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {

                        inversores.add(new Inversor((document.getId()),
                                document.getDouble("alt"),
                                document.getDouble("larg"),
                                document.getDouble("ef"),
                                document.getDouble("potencia"),
                                document.getString("marca"),
                                document.getString("empresa"),
                                document.getString("link"),
                                document.getBoolean("disponivel")));
                        Log.d("INVERSORES", "ID:" + document.getId() + " => " + document.getData(), task.getException());
                    }
                } else {
                    Log.d("INVERSORES", "Error getting documents", task.getException());
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(getContext(), "Error Inversor" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    // When I check my two List here their empty
    public void Calculate() {
        // Nothing relevant
    }

double lat = latlng.lat; double lng = latlng.lng; latlng.latitudelatlng.longitudelatlng.lat()会产生编译错误。

答案 4 :(得分:-2)

这是针对Android解决方案的

public void onMapClick(LatLng latln) {
    String s = latln.toString();
    String[] latLng = s.substring(10, s.length() - 1).split(",");
    String sLat = latLng[0];
    String sLng = latLng[1];
    Toast.makeText(MapsActivity.this, "Latitude is: "+sLat+", Longtitude is: "+sLng, Toast.LENGTH_LONG).show();
}