【问题标题】:latitude, longitude, string value纬度、经度、字符串值
【发布时间】:2014-12-12 18:57:30
【问题描述】:

我是 android 编程新手,我有一个项目,我想在我的地图中添加纬度和经度,我从 Web 服务中获取这些值,并将它们声明为字符串,但问题是在我的 java代码它不会用 LatLng 方法带它们,我尝试添加代码:

} else if (xpp.getName().equals(TAG_LATITUDE)) {
                performance.latitude = valueRead;
            } else if (xpp.getName().equals(TAG_LONGITUDE)) {
                performance.longitude = valueRead;

public String latitude;
public String longitude;


CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10);
    maps.animateCamera(cameraUpdate);

在我尝试了你们都告诉我的方法之后,我遇到了这个问题,地图什么也没显示,现在我添加了一些代码,在你们都告诉我方法之后我修改了:

我解析 xml 的类;

public double latitude;
public double longitude;

TAG_LATITUDE = "Latitude", TAG_LONGITUDE = "Longtitude",

} else if (xpp.getName().equals(TAG_LATITUDE)) {
                performance.latitude = Double.parseDouble(valueRead);
            } else if (xpp.getName().equals(TAG_LONGITUDE)) {
                performance.longitude = Double.parseDouble(valueRead);

现在是我使用地图的班级;

public class KartelaFragment extends Fragment {

private double lat;
private double lng;

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

    mapView = (MapView) k.findViewById(R.id.harta);
    mapView.onCreate(savedInstanceState);

    maps = mapView.getMap();
    maps.getUiSettings().setMyLocationButtonEnabled(false);
    maps.setMyLocationEnabled(true);

    MapsInitializer.initialize(this.getActivity());

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10);
    maps.animateCamera(cameraUpdate);

    MarkerOptions marker = new MarkerOptions();
    marker.title(String.valueOf(klienti));
    marker.snippet(String.valueOf(adresa));
    marker.draggable(true);
    marker.position(new LatLng(lat, lng));
    maps.addMarker(marker);
    maps.getUiSettings().setAllGesturesEnabled(true);
    maps.getUiSettings().setCompassEnabled(true);

lat = (double) k.findViewById(R.id.lati);
    lng = (double) k.findViewById(R.id.longi);




    return k;


void shfaqPerformance(ClientPerformanceList.ClientPerformance performance) {
    lat = Double.parseDouble(String.valueOf(performance.latitude));
    lng = Double.parseDouble(String.valueOf(performance.longitude));

【问题讨论】:

  • 看看我的回答。可能对你有帮助。

标签: android google-maps


【解决方案1】:

this

LatLng 需要 double 值,但您传递了 Strings。您需要将它们转换为doubles,使用Double.parseDouble

【讨论】:

    【解决方案2】:

    您将始终获得 double 类型的 Lat-Long 值。因此,如果您想访问它,您必须将您的 String 值解析为 Double 值,如下所示:

    因此,将您的纬度和经度值类型更改为 Double 并将其传递如下:

    public double latitude;
    public double longitude;
    
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10);
        maps.animateCamera(cameraUpdate);
    

    如果您以String 的形式从您的网络服务获取经纬度值,则将其分配给您的变量,如下所示:

    public double latitude= Double.parseDouble(performance.latitude);
    public double longitude= Double.parseDouble(performance.longitude);
    

    【讨论】:

    • 嗯,这很有帮助,但它给了我一些其他错误,我应该在哪里讨论它??你有任何电子邮件或其他我可以交谈的地方吗?
    • @DevilVl 您只能在问题中直接发布错误。我会为您提供解决方案。
    • 好吧,我已经修改了值,我把它加倍了,现在我有了这个:“lat = Double.parseDouble(String.valueOf(performance.latitude)); lng = Double.parseDouble(String .valueOf(性能.经度));"但现在我再也看不到地图了,纬度和经度也不会像以前那样显示了:(
    • @DevilVl 我认为你的performance.latitude 值已经是字符串格式了?
    • 我会在 parse xml 和我工作的 java 类中向你发送完整的代码,
    【解决方案3】:

    这样试试

                    if (jsonresult == true) {
                        JSONArray Jarray = object.getJSONArray("data");
                        for (int i = 0; i < Jarray.length(); i++) {
    
                            JSONObject Jasonobject = Jarray.getJSONObject(i);
                            latitude = Jasonobject.getString("latitude");
                            longitude = Jasonobject.getString("longitude");
                            speed=Jasonobject.getString("speed");
                        }
                        double lat=Double.parseDouble(latitude);
                        double lng=Double.parseDouble(longitude);
    
                        CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10);
                          maps.animateCamera(cameraUpdate);
    

    【讨论】:

      猜你喜欢
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2020-01-26
      • 2023-03-28
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多