如何通过点击listview项目打开谷歌地图? [英] How to open a Google map by clicking on listview item?

查看:84
本文介绍了如何通过点击listview项目打开谷歌地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何在Android Studio中使用地图的人提供帮助。我对如何通过点击Fragment中的listview项目来打开G.Map感兴趣。这是我的代码:



这是来自Fragment的代码:



I need help from someone who knows how to use maps in Android Studio. I'm interested in how to open a G.Map by clicking on a listview item from Fragment.This is my code:

This is code from Fragment:

@OnItemClick(R.id.lvTours)
    public void onItemClick(int position) {


        String tour = ((Tours) lvTours.getAdapter().getItem(position)).getCoord();//coordinats are stored on Firebase
        Intent showMap = new Intent(getContext(), MapActivity.class);
        showMap.putExtra(MapActivity.COORD, longitude);
        showMap.putExtra(MapActivity.COORD, latitude);
        showMap.putExtra(MapActivity.COORD, tour);
        startActivity(showMap);

    }





这是MapActivity:





And this is MapActivity:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback, LocationSource.OnLocationChangedListener {


    public static final String COORD = "coord";
    private GoogleMap.OnMapClickListener mCustomOnMapClickListener;

    private GoogleMap mGoogleMap;
    LocationManager mLocationManager;
    private Criteria mCriteria;
    private String mShowMap;


    private LocationSource.OnLocationChangedListener mListener;
    @BindView(R.id.lvTours)
    ListView lvTours;
    private SupportMapFragment mSupportMapFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tours_coord);
        this.mSupportMapFragment = (SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.fGoogleMap);
        this.mSupportMapFragment.getMapAsync(this);
        this.mCriteria = new Criteria();
        this.mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        this.mLocationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                MarkerOptions newMarkerOptions = new MarkerOptions();
                newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
                newMarkerOptions.title("Tour");
                newMarkerOptions.snippet("It' was here!");
                newMarkerOptions.position(latLng);
                mGoogleMap.addMarker(newMarkerOptions);

                this.handleStartingIntent();

            }

            private void handleStartingIntent() {
                Bundle bundle = getIntent().getExtras();
                if (bundle.containsKey(COORD)) {
                    mShowMap = bundle.getString(COORD);

                }


            }
        };
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.mGoogleMap = googleMap;
        UiSettings uiSettings = this.mGoogleMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(true);
        uiSettings.setMyLocationButtonEnabled(true);
        uiSettings.setZoomGesturesEnabled(true);
        this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
        // goToLocation(33.835293 , -117.914505);
    }


    @Override
    public void onLocationChanged(Location location) {


        if (mListener != null) {
            mListener.onLocationChanged(location);

            //Move the camera to the user's location and zoom in!
            mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 12.0f));
        }


    }


    public void goToLocation(double lat, double lng) {

        LatLng latLng = new LatLng(lat, lng);


        CameraPosition position = CameraPosition.builder()
                .target(latLng)
                .zoom(16f)
                .bearing(0.0f)
                .tilt(0.0f)
                .build();

        mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), null);


    }
}





如果有人愿意,我将不胜感激我是一个如何解决这个问题的例子。谢谢。



我尝试了什么:



它试图找到一个使用Google的解决方案。



I would be grateful to anyone who would make me an example on how to solve this.Thanks.

What I have tried:

It tried to found a solution using Google.

推荐答案

我可以想到打开地图的最小代码量如下:

The minimum amount of code I can think of to open a map looks like:
activity_maps.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Intent intent = new Intent(this, MapsActivity.class);
        intent.putExtra("LATITUDE", 38.65452);
        intent.putExtra("LONGITUDE", -90.18471);
        startActivity(intent);
    }
}

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
{
    private double dLatitude = 0.0;
    private double dLongitude = 0.0;

    private GoogleMap mMap;

    //================================================================

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        Bundle bundle = getIntent().getExtras();
        dLatitude = bundle.getDouble("LATITUDE");
        dLongitude = bundle.getDouble("LONGITUDE");

        // 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);
    }

    //================================================================

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

        LatLng latlng = new LatLng(dLatitude, dLongitude);
        mMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    }
}

您可以从此处添加。


这篇关于如何通过点击listview项目打开谷歌地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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