我在我的项目中寻找Google地图应用程序,我没有问题来获取地图和GPS位置。但是,当我尝试创建一个搜索栏以在地图上找到某个位置时,我无法制作它并且应用程序停止。当我开始调试时,我得到了这个:
06-27 14:31:52.753 17791-17791 / com.example.thier.sherpa E / AndroidRuntime:致命异常:主要 处理:com.example.thier.sherpa,PID:17791 java.lang.IllegalStateException:找不到方法 onMapSearch(View)在父或祖先上下文中为android:onClick 在视图类上定义的属性 android.support.v7.widget.AppCompatButton,id为'search_button' 在 android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
这是MapActivity.java的代码
package com.example.thier.sherpa;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
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 onMapSearch(View view) {
EditText locationSearch = (EditText) findViewById(R.id.editText);
String location = locationSearch.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(27.746974, 85.301582);
mMap.addMarker(new MarkerOptions().position(sydney).title("Kathmandu, Nepal"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
}
以下是XML代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="Search Location Here" />
<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:onClick="onMapSearch"
android:text="Search" />
</LinearLayout>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
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.viralandroid.googlemapsandroidapi.MapsActivity" />
答案 0 :(得分:0)
You can use PlaceAutocomplete on click of Editext in your app to find a
location Latitude and Longitude and point it out on Google Map.
/*** Use the Code Snipet***/
locationSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try
{
Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN).build(Dosage_Calculator.this);
startActivityForResult(intent, 1);
}
catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1)
{
if (resultCode == RESULT_OK)
{
Place place = PlaceAutocomplete.getPlace(MapsActivity.this, data);
locationSearch.setText(place.getName());
/* Log.i(TAG, "Place: " + place.getName());
Log.i(TAG, "Place: " + place.getLatLng());
Log.i(TAG, "Place: " + place.getAddress());*/
}
else if (resultCode == PlaceAutocomplete.RESULT_ERROR)
{
Status status = PlaceAutocomplete.getStatus(MapsActivity.this, data);
Log.i(TAG, "Status: "+status.getStatusMessage());
}
}
}
});
And in your AndroidManifest.xml
Don't forget to add
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Your google_search_api_key"/>
Note : To create google_search_api_key : Use Link --> https://console.developers.google.com/