附近位于Android的Geo位置?

时间:2016-06-15 17:10:04

标签: android google-maps location

有10个地理位置(经度和纬度),应根据我当前位置与第一个最近的地理位置之间的距离指示标记,从该地理位置到下一个附近位置,反之亦然。对于此特定情况任何谷歌地图API支持或我们需要编写自己的算法。如果有的话请告诉我。

先谢谢

1 个答案:

答案 0 :(得分: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) {
        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();

        LatLng sydney = new LatLng(-34, 151);
        mPerth = mMap.addMarker(new MarkerOptions()
                .position(PERTH)
                .title("Perth")
                .snippet("Population: Perth")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

        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.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        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();
    }
}