17

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

Valid XHTML.

After click on SELECT THIS BUTTON, below window opens with Select button is disabled

Valid XHTML.

Please let me know, If there is any other best solution. Code reply will be much helpful or reference link.

Vamsi Abbineni
  • 479
  • 3
  • 7
  • 23
Dr_Dang
  • 452
  • 6
  • 15
  • I've got a user with the same problem at the moment so keen to find a solution to this too. What version of Google Play Services is installed? – DilbertDave Aug 23 '16 at 19:26
  • I've just replicated this after removing the development (debug) build from my phone and installing from the Play Store. Maybe an API key issue? – DilbertDave Aug 23 '16 at 19:35
  • I don't see any error with your code as I checked from this [documentation](https://developers.google.com/places/android-api/placepicker#add). The SELECT button of the **Use this location?** pop-up dialog was always enabled as stated in this [related SO question](http://stackoverflow.com/questions/38930796/android-unable-pick-location-from-place-picker). Make sure that your internet connection has no problem. – abielita Aug 24 '16 at 08:11
  • @abielita - Internet connection is not the problem in my case. If I deploy to my devices using the debug key everything works as expected. If I deploy using the release key then I get the above behaviour but the maps are displayed fine in other parts of the application. – DilbertDave Aug 24 '16 at 09:16
  • I am using following packages - **1.)** com.android.support:appcompat-v7:24.2.0 **2.)** com.google.android.gms:play-services:8.3.0 **3.)** com.google.maps.android:android-maps-utils:0.3.4 – Dr_Dang Aug 25 '16 at 06:48
  • Have you checked the configuration of the api key in the manifest (see my answer below)? – DilbertDave Aug 29 '16 at 08:48
  • Just in case someone finds it useful: you have to insert the `` tag into ``, not into ``, otherwise it won't work (although it doesn't throw any build error) – Aron Lorincz May 03 '17 at 11:54

8 Answers8

8

Solution: Enable Google Places API for Android from dev console.

Victor Häggqvist
  • 4,484
  • 3
  • 27
  • 35
Xakiru
  • 2,556
  • 1
  • 15
  • 11
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.

DilbertDave
  • 3,406
  • 3
  • 33
  • 42
  • what did you actually do?how to change release configuration? – Vikas Pandey Dec 16 '16 at 12:57
  • @VikasPandey - as I mentioned I'm using Xamarin and Visual Studio so this may not apply to you. I have a separate GoogleMapsKey.cs file which contains two MetaDataAttributes, one contains my debug api key and the other has the release key. I switch between them with a conditional compilation symbol, e.g. #if DEBUG. You can see an example here: https://github.com/adrianstevens/XamarinAndroidFlyoutMaps/blob/master/ActionBarMap/GoogleMapsKey.cs – DilbertDave Dec 16 '16 at 13:29
0

I faced the same problem and it got resolved when I enabled Google Places API for Android API in my google cloud project.

Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
0

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

  1. Google API Console
  2. Identify the Android Key the project is using
  3. Select Android OS, enter the package name and SHA1
  4. Check the google-services.json (from Firebase), it should be using this key.

Rebuild, and everything works well.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
0

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>
Marek Urbanowicz
  • 12,659
  • 16
  • 62
  • 87
Dhara Patel
  • 359
  • 5
  • 19
0
  1. Make sure you have enabled this API in console.developers.google.com
  2. 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.
pro2on
  • 1
  • 2
0
 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);
    }
0

I also had to enable Geocoding API. These are the APIs that I'm using:

enter image description here

Ricardo
  • 2,086
  • 25
  • 35