PlacePicker不会随位置启动

时间:2016-11-05 22:46:04

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

我正在使用API​​谷歌搜索项目。我使用PlacePicker,我跟踪手机以启动PlacePicker。这是我的位置和PlacePicker。

public class ProspectionActivity extends BaseActivity {
    private static final int PLACE_PICKER_REQUEST = 1;
    private TextView mName;
    private TextView mAddress;
    private TextView mAttributions;
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(48.8453849, 2.328134900000009), new LatLng(48.866667, 2.333333));
                  public double latitude;
    public double longitude;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame); //Remember this is the FrameLayout area within your activity_base.xml
    getLayoutInflater().inflate(R.layout.activity_prospection, contentFrameLayout);
    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!enabled) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location!=null){
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        final LatLngBounds coordinate = new LatLngBounds(new LatLng(latitude-0.000005, longitude-0.000005),new LatLng(latitude+0.000005,longitude+0.000005));
        mName = (TextView) findViewById(R.id.textView);
        mAddress = (TextView) findViewById(R.id.textView2);
        mAttributions = (TextView) findViewById(R.id.textView3);
        Button pickerButton = (Button) findViewById(R.id.pickerButton);
        pickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    PlacePicker.IntentBuilder intentBuilder =
                            new PlacePicker.IntentBuilder();
                    intentBuilder.setLatLngBounds(coordinate);
                    Intent intent = intentBuilder.build(ProspectionActivity.this);
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);

                } catch (GooglePlayServicesRepairableException
                        | GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {

    if (requestCode == PLACE_PICKER_REQUEST
            && resultCode == Activity.RESULT_OK) {

        final Place place = PlacePicker.getPlace(this, data);
        final CharSequence name = place.getName();
        final CharSequence address = place.getAddress();
        String attributions = (String) place.getAttributions();
        if (attributions == null) {
            attributions = "";
        }

        mName.setText(name);
        mAddress.setText(address);
        mAttributions.setText(Html.fromHtml(attributions));

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

} `
不幸的是,当我点击按钮启动PlacePicker时。什么都没发生。如果有人可以帮助我。

这是我的清单                    

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".BaseActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name=".ProspectionActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" />
    <meta-data

        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyAfOvxBfhFLmQEgopaNIonahIYaR9jVKrk" />

</application>

1 个答案:

答案 0 :(得分:1)

在logcat输出中查找可以告诉您发生了什么的错误可能很有用。

查看您的清单,我发现您尚未请求ACCESS_FINE_LOCATION权限。这可能是问题所在。尝试添加:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />