我想在地图片段和其他片段中显示当前位置和特定的附近地点(例如医院)

时间:2018-06-10 14:46:05

标签: android google-maps android-layout

我在导航抽屉里面制作了地图片段,它的工作非常精细,但它没有显示当前位置。另外我想在导航抽屉按钮点击事件上显示附近的地方。 我的应用程序设计看起来像http://prntscr.com/jta0i5

地图片段代码

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import youtube.demo.youtubedemo.R;

import static com.google.android.gms.maps.model.BitmapDescriptorFactory.*;

public class GmapFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;
    private static final int Location_Request = 500;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_gmaps, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        mMap.getUiSettings().setZoomControlsEnabled(true);



         LatLng pindora = new LatLng(33.6498,73.0686);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pindora, 13));

        googleMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(pindora));



    }



}

显示医院折扣代码

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;

import youtube.demo.youtubedemo.R;

import static com.google.android.gms.maps.model.BitmapDescriptorFactory.*;

public class ImportFragment extends Fragment implements OnMapReadyCallback  {

    private GoogleMap mMap;
    private static final int Location_Request = 500;

    private static final LatLng PERTH = new LatLng(-31.952854, 115.857342);
    private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689);
    private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235);

    private Marker mPerth;
    private Marker mSydney;
    private Marker mBrisbane;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_import, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        mMap.getUiSettings().setZoomControlsEnabled(true);

        LatLng Hospital1  = new LatLng(33.633753,73.078982);
        LatLng Hospital2  = new LatLng(33.642020, 73.057642);
        LatLng Hospital3  = new LatLng(33.627556, 73.071613);
        LatLng Hospital4  = new LatLng(33.645950, 73.113775);
        LatLng Hospital5  = new LatLng(33.640071, 73.062459);
        LatLng Hospital6  = new LatLng(33.677361, 73.070699);


        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital1, 13));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital2, 13));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital3, 13));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital4, 13));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital5, 13));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Hospital6, 13));



        googleMap.addMarker(new MarkerOptions().title("Bilal Hospital!").position(Hospital1));
        googleMap.addMarker(new MarkerOptions().title("Holy Family!").position(Hospital2));
        googleMap.addMarker(new MarkerOptions().title("Benazir Bhutto Hospital").position(Hospital3));
        googleMap.addMarker(new MarkerOptions().title("Abdul Sattar Family Hospital").position(Hospital4));
        googleMap.addMarker(new MarkerOptions().title("Madina Medical Centre").position(Hospital5));
        googleMap.addMarker(new MarkerOptions().title("Shifa International Hospital").position(Hospital6));

    }

}

在这两个文件中,我必须手动输入所有位置Lat和Lng

0 个答案:

没有答案