简体   繁体   中英

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

I'm currently working in Android Studio. Below is the code in my java file, so far I'm able to find a location based on an input which I specified within the code. I would instead like to have 2 separate edit text locations where I can input the longitude and latitude, then press a button to find the location.

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

I'll also include my activity main in case it's any help.

<?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>

I'm really not sure how to do this so any help would be appreciated :)

What you need to do, is connect your TextView's in the Activity itself and then have a button action to perform the action you set, this is a rough example.

Just update your textviews to be EditText, and add a button with the same Id as in the findViewById()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM