【问题标题】:start navigating to position which is stored in database开始导航到存储在数据库中的位置
【发布时间】:2013-05-15 09:49:17
【问题描述】:

我有一个获取纬度和经度坐标的应用程序。

我想,什么时候按下按钮开始导航到那个位置。

现在,我将经纬度存储在数据库中。

所以,我想先提取位置。当我按下按钮进行导航时,我会打开一个警报对话框,然后在“是”中进行操作:

public void navigation(final View v){



     AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        alt_bld.setMessage("Do you want to navigate to the saved position?")
                .setCancelable(false)
                .setPositiveButton("Navigate",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                        // Action for 'Yes' Button

                        String query = "SELECT latitude,longitude FROM MEMORIES ";
                        Cursor c1 = sqlHandler.selectQuery(query);

                        if (c1 != null && c1.getCount() != 0) {
                            if (c1.moveToFirst()) {
                                do {                
        String mylatitude=c1.getString(c1.getColumnIndex("latitude"));
        String mylongitude=c1.getString(c1.getColumnIndex("longitude"));

        Double lat=Double.parseDouble(mylatitude);
        Double lon=Double.parseDouble(mylongitude); 


                                    } while (c1.moveToNext());
                                }
                            }

                            c1.close();


            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
                    Uri.parse("http://maps.google.com/maps?saddr=" + 
                    gps.getLocation().getLatitude() + "," + 
                    gps.getLocation().getLongitude() + "&daddr=" + mylatitude + "," + mylongitude ));
                    startActivity(intent);


                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // Action for 'NO' Button
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = alt_bld.create();
        // Title for AlertDialog
        alert.setTitle("Navigation");
        alert.show();

 }

我存储了位置(纬度和经度),我想开始导航到该位置。 我该怎么做?

谢谢!

----------更新-------------- ------------

如果我喜欢:

    String mylat="";
String mylon="";


@Override
protected void onCreate(Bundle savedInstanceState) {
...
...
public void navigation(final View v){
....
String mylatitude=c1.getString(c1.getColumnIndex("latitude"));
String mylongitude=c1.getString(c1.getColumnIndex("longitude"));


mylat=mylatitude;//stored coordinates from database 
mylon=mylongitude;

String f="45.08" //current location (start points)
String s="23.3";

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr=" + 
f + "," + 
s + "&daddr=" + mylat + "," + mylon ));
startActivity(intent);

意图开始,我在地图应用程序中,起点是我在上面定义的“f”和“s”,但目标点是 0.0 , 0.0 ;

所以,我有两个问题:

1)如何将我存储的位置(我的纬度,我的经度(我复制到 mylat,mylon)放到目的地点

2) 如何获取当前位置(初始点),因为我的 gps 类对此不起作用。

【问题讨论】:

  • 您必须将它们作为字符串保存在数据库中吗?最好存储为两个单独的浮点数并根据需要制作字符串。
  • @Gustek:我更愿意将它们保留为字符串。但我对解决方案持开放态度。一般来说,我不知道这一切是否适用于导航到保存的位置。

标签: android database


【解决方案1】:

在下面的示例中,ImplLocationService 是我的应用程序中的一个类/服务,它提供设备内当前位置的纬度和经度坐标。 Location 类与 Android 的产品类似,但略有不同,它提供目的地纬度和经度坐标。如果您要从数据库中提取这些值,则以下方法是相同的。

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
               Uri.parse("http://maps.google.com/maps?saddr=" + 
               ImplLocationService.getCurrentLocation().getLatitude() + "," + 
               ImplLocationService.getCurrentLocation().getLongitude() + "&daddr=" + 
               location.getPoint().latitude + "," + location.getPoint().longitude));
        startActivity(intent);

【讨论】:

  • :首先感谢您的帮助!所以,我应该放 "lat" 而不是 location.getPoint().latitude (因为我在 Double lat=Double.parseDouble(mylatitude) 上面放了它;) ,对吗?但是 lat 和 lon 不能像我所拥有的那样被解析为变量。我不知道该怎么做..
  • @George 你应该能够复制我所拥有的并放入一个固定的起始纬度和经度位置,而不是我使用ImplLocationService。这样做之后,将location.getPoint().latitude 替换为您的字符串值不加倍,应该这样做。我不确定为什么您的变量不在范围内,我需要查看所有代码才能确定。
  • :我更新了。我输入了字符串值“mylatitude”和“mylongitude”,但仍然无法解析。
  • :通过 gps.getLocation().getLatitude() 等,我调用了一个查找纬度和经度的服务。
  • 从固定的纬度和经度点开始,让 Intent 工作,然后担心拉动动态值。什么是错误或正在发生的事情与您预期的不一样?
【解决方案2】:

同时以这种方式存储经纬度...

String mylocation=latitude + ":" + longitude;

在检索它的同时

String latitude = mylocation.subString(0, myLocation.indexOf(":") -  1 );
String longitude = mylocation.subString(myLocation.indexOf(":"));

并将其传递为

j.putExtra("lon", longitude);
j.putExtra("lat", latitude);

【讨论】:

  • 您好,问题是我需要另一个活动中的位置。我不能使用 mylocation.subString..另外,它是 mylocation 而不是 myLocation ,对吗?
  • :有什么想法吗?谢谢!
  • bakriOnFire 在这点上是对的。如果您希望在另一个活动中使用它,请将其添加到意图中,然后在第二个活动中从意图中检索它并按照此答案继续。
【解决方案3】:

一种可能的方法是将您的 latitudelongitude 值存储在共享首选项中。

-存储可以使用的值

 SharedPreferences sp = getSharedPreferences("MyPrefs", MODE_PRIVATE);

        Editor editor = sp.edit();
        editor.putString("mytext", text);
        editor.commit();

-并检索您可以使用的值

String value = prefs.getString("MyPrefs, "mytext");

在您的代码中,您将纬度和经度存储在 double 中,不要忘记将它们转换为 toString()...

希望对你有帮助

【讨论】:

  • :谢谢,但我正在保存到数据库中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2018-09-28
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多