How to make a marker to a location in Google Maps Activity?

时间:2016-07-11 20:01:59

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

I'm making an app with a Google Maps Activity and I want to place a marker there. How do I set a marker to a location, for example the White House, so that it opens up the info page in google maps when you press the maps button in the right corner? My code:

LatLng latLng = new LatLng(38.897677, -77.036531);
    mMap.addMarker(new MarkerOptions().position(latLng).title("The White House"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

2 个答案:

答案 0 :(得分:1)

以下是您的问题的答案我认为您实际上想要实现点击监听器。

有两种方法可以做到这一点

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener( ) {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(StoreMap.this, "Info window clicked", Toast.LENGTH_SHORT).show();
    }
如果您打算仅通过单击标记来指导用户,请使用此文件。

GoogleMap.setOnMarkerClickListener(OnMarkerClickListener)

按照与上面类似的方式实施 more details

答案 1 :(得分:0)

您希望自己的应用搜索地点吗?尝试地理编码。这是我的超级简化搜索方法:

public void onSearch(String searchLocation) {
    mGoogleMap.clear();
    List<Address> addressList = null;
    Address address = null;
    LatLng latLng = null;

    Geocoder geocoder = new Geocoder(mContext);
    try {
        addressList = geocoder.getFromLocationName(searchLocation, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    address = addressList.get(0);
    latLng = new LatLng(address.getLatitude(), address.getLongitude());
    mGoogleMap.addMarker(new MarkerOptions().position(latLng).title(searchLocation));
    mGoogleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}