簡體   English   中英

如何根據用戶輸入的經度和緯度找到位置?

[英]How do I find a location based on the longitude and latitude that the user enters?

我目前在Android Studio中工作。 以下是我的java文件中的代碼,到目前為止,我已經能夠根據我在代碼中指定的輸入來找到位置。 相反,我希望有2個單獨的編輯文本位置,可以在其中輸入經度和緯度,然后按一個按鈕查找位置。

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.osmdroid.views.MapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.config.Configuration;

public class HelloMap extends Activity
{

    MapView mv;

    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_map);


        Configuration.getInstance().load
                (this, PreferenceManager.getDefaultSharedPreferences(this));

        mv = (MapView)findViewById(R.id.map1);

        mv.setBuiltInZoomControls(true);
        mv.getController().setZoom(14);
        mv.getController().setCenter(new GeoPoint(51.05,-0.72));
    }}

如果有任何幫助,我還將包括我的活動主信息。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <org.osmdroid.views.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:enabled="true"
        android:clickable="true"
        android:id="@+id/map1"
        tilesource="Mapnik"
        />

    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:id="@+id/latet"
    />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/longet"
        android:layout_below="@id/latet"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lattv"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/longtv"

        />
</RelativeLayout>

我真的不知道如何做到這一點,所以任何幫助將不勝感激:)

您需要做的是將TextView連接到Activity本身,然后執行一個按鈕動作來執行您設置的動作,這是一個粗糙的示例。

只需將您的textview更新為EditText,並添加一個與findViewById()中相同的ID的按鈕即可。

MapView mv;
EditText latitude;
EditText longitude;

Button showLocationButton;

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hello_map);


    Configuration.getInstance().load
            (this, PreferenceManager.getDefaultSharedPreferences(this));

    mv = (MapView)findViewById(R.id.map1);
    latitude = (EditText)findViewById(R.id.lattv);
    longitude = (EditText)findViewById(R.id.longtv);
    showLocationButton = (Button)findViewById(R.id.showlocationbtn);

    showLocationButton.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    showLocation(Double.parseDouble(latitude.getText().toString()),  Double.parseDouble(longitude.getText().toString()));
                }
            })

    mv.setBuiltInZoomControls(true);
    mv.getController().setZoom(14);
    mv.getController().setCenter(new GeoPoint(51.05,-0.72));
}}

private void showLocation(double latitude, double longitude)
{
    mv.getController().setCenter(new GeoPoint(latitude, longitude));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM