Android Place Picker

时间:2017-03-19 01:10:11

标签: android google-maps google-places-api

我无法在我的应用中使用地方选择器。 API工作然后停止了一天没有任何解释。选择器显示很短的时间然后只是关闭,没有例外,日志中没有任何内容。我还为Android启用了google places api。

主要活动

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
int PLACE_PICKER_REQUEST = 1;

private Button add;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 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);

    add = (Button) findViewById(R.id.add);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

            try {
                startActivityForResult(builder.build(MainActivity.this), PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });
}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (mMap == null)
        return;

    MarkerOptions options = new MarkerOptions();
    //Need to add new lat long here
    ArrayList<LatLng> latlngs = new ArrayList<>();
    latlngs.add(new LatLng(12.334343, 33.43434));
    latlngs.add(new LatLng(28.704059, 77.102490));
    latlngs.add(new LatLng(19.075984, 72.877656));

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    for (LatLng point : latlngs) {
        options.position(point);
        options.title("someTitle");
        options.snippet("someDesc");
        googleMap.addMarker(options);
    }
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
    }
}

}

清单

<?xml version="1.0" encoding="utf-8"?>

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

这很可能是API密钥/配额问题。调用(if)onActivityResult()时会得到什么resultCode?

您可以尝试启用“地方信息”的详细日志记录,这可能会记录可让您更深入了解问题原因的内容

来自here

$ adb shell setprop log.tag.Places DEBUG