如何在Google地图上显示PlacePicker的位置? [英] How to show PlacePicker location on a Google Map?

查看:123
本文介绍了如何在Google地图上显示PlacePicker的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用Google Maps API和Google Places API.在我搜索某个位置,然后从结果中选择它之前,一切工作正常,直到我搜索了一个位置,然后会出现一个对话框(不应显示),并提示用户是否要选择该位置或更改地址.点击选择"后,该应用将删除地点用户界面,并变成常规地图,而无需前往所选地点.我的代码如下.

My app uses the google maps api and the google places api. Everything works perfectly until I search for a certain place, when I search for a place and then select it from the results a dialog box (which should not show) shows up and prompts the user if they want to select that place or change the address. After clicking "select" the app removes the places UI and turns into a regular map, without ever going to the place selected. My code is below.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private static final int PLACE_PICKER_REQUEST = 1000;
    private GoogleApiClient mClient;
    @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);


        int PLACE_PICKER_REQUEST = 1;
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();


        try {
            startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
        mClient = new GoogleApiClient
                .Builder(this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .build();

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);
            StringBuilder stBuilder = new StringBuilder();
            String placename = String.format("%s", place.getName());
            String latitude = String.valueOf(place.getLatLng().latitude);
            String longitude = String.valueOf(place.getLatLng().longitude);
            String address = String.format("%s", place.getAddress());
            stBuilder.append("Name: ");
            stBuilder.append(placename);
            stBuilder.append("\n");
            stBuilder.append("Latitude: ");
            stBuilder.append(latitude);
            stBuilder.append("\n");
            stBuilder.append("Logitude: ");
            stBuilder.append(longitude);
            stBuilder.append("\n");
            stBuilder.append("Address: ");
            stBuilder.append(address);

        }
    }
}

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
    @Override
    protected void onStart() {
        super.onStart();
        mClient.connect();
    }
    @Override
    protected void onStop() {
        mClient.disconnect();
        super.onStop();
    }

}

推荐答案

您需要使用data目的获取用户选择的地方的位置,在地图上添加标记,然后移动相机它位于所选地方的中央:

You need to use the data Intent to get the location of the Place that the user selected, add a Marker to the map, and move the camera so that it's centered over the selected Place:

@Override
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 placeName = String.format("Place: %s", place.getName());
            String placeAddress =  String.format("Address: %s", place.getAddress());
            LatLng toLatLng = place.getLatLng();

            // Add Marker
            marker = mMap.addMarker(new MarkerOptions().position(toLatLng)
                    .title(placeName).snippet(placeAddress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            // Move Camera to selected place
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));

        }
    }
}

这篇关于如何在Google地图上显示PlacePicker的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆