基于Android的基于邻近位置的地理位置?

问题描述 投票:1回答:1

有10个地理位置(经度和纬度),应根据我当前位置到最近的地理位置之间的距离(从该地理位置到下一个附近位置,反之亦然)进行标记。对于这种特殊情况,任何Google地图API支持,否则我们需要编写自己的算法。如果有请让我知道。

预先感谢

android google-maps location
1个回答
0
投票
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@android:color/white">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:tint="@android:color/white"
            android:backgroundTint="#1baec3"
            app:fabSize="auto"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="33.8dp"
            android:layout_marginRight="13.5dp"
            app:srcCompat="@drawable/search" />

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/padding_margin_20"/>

        <RelativeLayout
            android:id="@+id/rl_top"
            android:layout_width="match_parent"
            android:layout_height="69dp"
            android:layout_alignParentTop="true"
            android:background="@drawable/gradient_gray" >
            <ImageView
                android:id="@+id/img_toggle"
                android:layout_width="8.5dp"
                android:layout_height="8.5dp"
                android:layout_centerVertical="true"
                android:src="@drawable/icon_toggle"
                android:tint="@color/text_color"
                android:scaleType="fitXY" />
            <TextView
                android:id="@+id/tv_search_directory_near"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_toggle"
                android:layout_centerVertical="true"
                android:layout_marginLeft="@dimen/padding_margin_5"
                android:text="@string/near"
                android:textSize="9sp"
                android:textColor="#797979"/>
            <LinearLayout
                android:id="@+id/ly_map_radius"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="11.5dp"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/tv_search_directory_near"
                android:background="@drawable/bg_radius_black_line"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="10.3dp"
                    android:layout_height="6dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="9.5dp"
                    android:layout_marginTop="9.3dp"
                    android:layout_marginBottom="9.8dp"
                    android:src="@drawable/down_arrow"
                    android:tint="#797979"/>

                <TextView
                    android:id="@+id/tv_map_radius"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6.3dp"
                    android:layout_marginTop="9.3dp"
                    android:layout_marginBottom="8.8dp"
                    android:layout_marginRight="11dp"
                    android:text="@string/spinner_radius"
                    android:textSize="9sp"
                    android:fontFamily="@font/roboto"
                    android:textColor="#797979"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/ly_map_country"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10.8dp"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/ly_map_radius"
                android:background="@drawable/bg_radius_black_line"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="10.3dp"
                    android:layout_height="6dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="9.5dp"
                    android:layout_marginTop="9.3dp"
                    android:layout_marginBottom="9.8dp"
                    android:src="@drawable/down_arrow"
                    android:tint="#797979"/>

                <TextView
                    android:id="@+id/tv_map_country"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6.3dp"
                    android:layout_marginTop="9.5dp"
                    android:layout_marginBottom="@dimen/padding_margin_7"
                    android:layout_marginRight="@dimen/padding_margin_15"
                    android:text="@string/spinner_country"
                    android:textSize="9sp"
                    android:textColor="#1baec3"/>
            </LinearLayout>
        </RelativeLayout>
    </RelativeLayout>



public class Map_Fragment extends Fragment implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback, View.OnClickListener{
    private GoogleMap mMap;
    LatLng markerLocation;
    Projection projection;
    Point screenPosition;

    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;

    LinearLayout ly_map_radius, ly_map_country;
    TextView tv_map_radius, tv_map_country;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_map, container, false);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        ly_map_radius = root.findViewById(R.id.ly_map_radius);
        ly_map_country = root.findViewById(R.id.ly_map_country);

        tv_map_radius = root.findViewById(R.id.tv_map_radius);
        tv_map_country = root.findViewById(R.id.tv_map_country);

        return root;
    }
    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ly_map_radius.setOnClickListener(this);
        ly_map_country.setOnClickListener(this);
    }

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

        projection = mMap.getProjection();

        // 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"));
        mPerth = mMap.addMarker(new MarkerOptions()
                .position(PERTH)
                .title("Perth")
                .snippet("Population: Perth")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        mPerth.showInfoWindow();
//        markerLocation = mPerth.getPosition();
//        projection.toScreenLocation(markerLocation);

//        screenPosition = projection.toScreenLocation(markerLocation);

        mSydney = mMap.addMarker(new MarkerOptions()
                .position(SYDNEY)
                .title("Sydney")
                .snippet("Population: Sydney")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        mSydney.showInfoWindow();

        mBrisbane = mMap.addMarker(new MarkerOptions()
                .position(BRISBANE)
                .title("Brisbane")
                .snippet("Population: Brisbane")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
        mBrisbane.showInfoWindow();

        mMap.setOnMarkerClickListener(this);
//        mMap.addMarker(new MarkerOptions()
//                .position(sydney)
//                .title("Sydney")
//                .snippet("Population: 4,627,300"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        // Retrieve the data from the marker.
//        Integer clickCount = (Integer) marker.getTag();
//
//        // Check if a click count was set, then display the click count.
//        if (clickCount != null) {
//            clickCount = clickCount + 1;
//            marker.setTag(clickCount);
//            Toast.makeText(getContext(),
//                    marker.getTitle() +
//                            " has been clicked " + clickCount + " times.",
//                    Toast.LENGTH_SHORT).show();
//        }

        // Return false to indicate that we have not consumed the event and that we wish
        // for the default behavior to occur (which is for the camera to move such that the
        // marker is centered and for the marker's info window to open, if it has one).
        return false;
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        switch (v.getId()){
            case R.id.ly_map_country:
                builder.setTitle("Select Country");
                builder.setItems(countries, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tv_map_country.setText(countries[which]);
                    }
                });
                break;
            case R.id.ly_map_radius:
                builder.setTitle("Select Radius");
                builder.setItems(radius, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tv_map_radius.setText(radius[which] + " km radius");
                    }
                });
                break;
        }
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.