问题
I want to save user form information with location field. For location I want to open google map on some button click and location to be selected when user click on location over map and post-filled location into form.
I found place picker as related solution, So I have used Place Picker Google API and I am able to open google map, when I move arrow over preferred location and click on Select this location (Appearing black color with now coordinates showing under that).
Confirmation box opens with 2 option :
1.) Change location
2.) Select (Disabled mode)
I want to select anonymous location and return to main activity.
Below is my code :
private TextView get_place;
int PLACE_PICKER_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get_place = (TextView)findViewById(R.id.textView1);
get_place.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent;
try {
intent = builder.build(getApplicationContext());
startActivityForResult(intent,PLACE_PICKER_REQUEST );
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}
protected void onActivityResult( int requestCode , int resultCode , Intent data ){
if( requestCode == PLACE_PICKER_REQUEST)
{
if(resultCode == RESULT_OK)
{
Place place = PlacePicker.getPlace(data,this);
Double latitude = place.getLatLng().latitude;
Double longitude = place.getLatLng().longitude;
String address = String.valueOf(latitude)+String.valueOf(longitude);
get_place.setText(address);
}
}
}
Current location coordinates not visible
.
After click on SELECT THIS BUTTON, below window opens with Select button is disabled
.
Please let me know, If there is any other best solution. Code reply will be much helpful or reference link.
回答1:
Solution: Enable Google Places API for Android from dev console.
回答2:
I had the same issue (see comments in original post) and it turned out to be caused by a mismatch in the API key entry in the AndroidManifest.xml file.
I'm using Xamarin.Android but the principle should be the same in Java - but your mileage may vary.
Basically my app was using the Maps API and in my manifest I had the API key specified as com.google.android.maps.v2.API_KEY - and everything worked fine. When I implemented the Places Picker i needed to change the key name to com.google.android.geo.API_KEY (I did NOT have to change the key itself!). I had done this for the debug configuration but NOT for the release configuration - which still used the Maps key name. Hence building a releasable package was resulting in a manifest which would satisfy the Maps API but not Places.
Changing the release configuration has resolved my issue.
回答3:
I faced the same problem and it got resolved when I enabled Google Places API for Android API in my google cloud project.
回答4:
All answers are either old, or irrelevant. I tried 2 solutions, which worked for me.
Solution 1
compile 'com.google.android.gms:play-services:9.4.0'
compile 'com.google.android.gms:play-services-base:9.4.0'
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
compile 'com.google.android.gms:play-services-contextmanager:9.4.0'
compile 'com.google.android.gms:play-services-places:9.4.0'
compile 'com.google.android.gms:play-services-nearby:9.4.0'
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-ads:9.4.0'
compile 'com.google.android.gms:play-services-auth:9.4.0'
compile 'com.google.android.gms:play-services-gcm:9.4.0'
compile 'com.google.android.gms:play-services-analytics:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
Reference : googledocumentation
Solution 2
- Google API Console
- Identify the Android Key the project is using
- Select Android OS, enter the package name and SHA1
- Check the google-services.json (from Firebase), it should be using this key.
Rebuild, and everything works well.
回答5:
You should use this
<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>
instead of
<application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_API_KEY"/>
<application>
回答6:
- Make sure you have enabled this API in console.developers.google.com
- Also (this is was my case) make sure your API key is correct - copy package from gradle applicationId field. Good test will be if you disable restrictions from API key.
回答7:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==PLACE_PICKER_REQUEST|requestCode==REQUEST_CHECK_SETTING){
if(resultCode==RESULT_OK) {
mlocationUpdate = true;
// startLocationUpdate();
Place place = PlacePicker.getPlace(data, this);
String str_txt = place.getName().toString();
str_txt += "\n" + place.getAddress().toString();
placeMarkerOnMap(place.getLatLng());
mStringBuilder = new StringBuilder();
String mplacename = String.format("%s", place.getName());
mlatitude = (place.getLatLng().latitude);
mlongitude = (place.getLatLng().longitude);
String maddress = String.format("%s", place.getAddress());
mStringBuilder.append("NAME :");
mStringBuilder.append(mplacename);
mStringBuilder.append("\n");
Log.e("SRC-->",mplacename);
Log.e("THIS-->",maddress);
mStringBuilder.append("ADDRESS :");
mStringBuilder.append(maddress);
/*mStringBuilder.append("\n");*/
if (isPickUp==true){
edt_pickup.setText(mStringBuilder);
isPickUp=false;
}
else if (isDrop==true){
edt_drop.setText(mStringBuilder);
mlatitudeEnd = (place.getLatLng().latitude);
mlongitudeEND = (place.getLatLng().longitude);
isDrop=false;
}
}}
super.onActivityResult(requestCode, resultCode, data);
}
来源:https://stackoverflow.com/questions/39092825/disable-select-button-while-selecting-location-of-anonymous-location-using-place