如何实现(通过键入)两个地址(原点和目的地),并将它们作为标记放在地图上(googlemaps api)

时间:2016-07-25 21:38:56

标签: android android-studio google-maps-api-3 google-maps-markers

我是Android新手,并试图在我的应用两个文本区域中放置我可以输入地址来源和目的地)并且点击按钮,这些地址将在地图中显示为

我能够完成第一部分,这意味着在第一个文本字段中,我能够输入我的原始地址,然后通过单击按钮,该点出现在地图中。 我的问题是,当我输入"目的地地址"时,我的应用程序会崩溃,或者没有任何反应(意味着地图中没有新点)。

这是我的#34; MapsActivity"代码到目前为止

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

public void onSearch(View view)
{
    EditText location_tf = (EditText)findViewById(R.id.txtedit);
    String location =location_tf.getText().toString();
    List<Address> addressesList = null ;
    if (location != null || !location.equals(""))
    {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressesList = geocoder.getFromLocationName(location , 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        LinkedHashMap addressList;
        Address address = addressesList.get(0);
        LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng ));

    }

    EditText location_tf1 = (EditText)findViewById(R.id.txtedit2);
    String location1 =location_tf1.getText().toString();
    List<Address> addressesList1 = null ;
    if (location != null || !location.equals(""))
    {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressesList1 = geocoder.getFromLocationName(location , 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        LinkedHashMap addressList;
        Address address = addressesList1.get(0);
        LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
        mMap.addMarker(new MarkerOptions().position(latLng).title("Marker1"));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng ));

    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Areios Pagos Athens and move the camera
    LatLng white_tower = new LatLng(40.626459, 22.948419);
    mMap.addMarker(new MarkerOptions().position(white_tower).title("Marker in white tower"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(white_tower));
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);

这是我的&#34; Activity_maps.xml&#34;文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtedit"
    android:text="Origin address!"
    android:textStyle="bold|italic"
    android:textColor="#a6a5a5"
    android:textSize="15dp"
    android:textIsSelectable="false" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txtedit2"
    android:text="Destination address!"
    android:textStyle="bold|italic"
    android:textColor="#a6a5a5"
    android:textSize="15dp"
    android:textIsSelectable="false" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Find addresses"
    android:id="@+id/btnsearch"
    android:textColor="#ff0000"
    android:textSize="15dp"
    android:textStyle="bold"
    android:onClick="onSearch" />

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.elesar.googleaddresssearch.MapsActivity" />

因此,当我试图运行我的应用程序时,在第一个&#34;原始地址&#34;中输入一个地址。然后点击按钮&#34;查找地址&#34;,标记出现在地图上!另一方面,当我在&#34;目的地地址&#34;中键入地址时没有任何反应。任何建议????? 提前谢谢!

0 个答案:

没有答案