Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java地图片段直接将相机移动到当前位置

我用地图片段中的place picker制作了一个代码来显示附近的地方,但它不能直接将相机移动到我当前的手机位置,我必须用center = new lat long手动输入lat long,但我希望它直接根据手机位置移动

如何让相机直接移动到我的位置??谢谢你的帮助

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
    MapFragment mapFragment;
    GoogleMap gMap;
    MarkerOptions markerOptions = new MarkerOptions();
    CameraPosition cameraPosition;
    LatLng center, latLng;
    String title;

    public static final String ID = "id";
    public static final String TITLE = "name";
    public static final String LAT = "lat";
    public static final String LNG = "lng";

    private String url = "http://192.168.0.12/myjson/markers.php";

    String tag_json_obj = "json_obj_req";

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

        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

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

        center = new LatLng(-6.337800, 106.698990);
        cameraPosition = new CameraPosition.Builder().target(center).zoom(10).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        getMarkers();
    }

    private void addMarker(LatLng latlng, final String title) {
        markerOptions.position(latlng);
        markerOptions.title(title);
        gMap.addMarker(markerOptions);

        gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    // Fungsi get JSON marker
    private void getMarkers() {
        StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.e("Response: ", response.toString());

                try {
                    JSONObject jObj = new JSONObject(response);
                    String getObject = jObj.getString("wisata");
                    JSONArray jsonArray = new JSONArray(getObject);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        title = jsonObject.getString(TITLE);
                        latLng = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));

                        // Menambah data marker untuk di tampilkan ke google map
                        addMarker(latLng, title);
                    }

                } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Error: ", error.getMessage());
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

        AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    首先,你需要知道当前的位置。为了做到这一点,你必须使用GoogleApiClient。然后,您可以使用CameraPosition将地图运行到该位置

    下面的代码首先连接到Google Api客户端。在连接回调中,它请求当前位置1秒。最后,它会设置动画以映射到最后一个可用的位置

    @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            client = new GoogleApiClient.Builder(getContext())
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                        @Override
                        public void onConnected(@Nullable Bundle bundle) {
                            if (isResumed()) {
                                try {
                                    LocationRequest request = new LocationRequest();
                                    request.setInterval(1000);
                                    request.setSmallestDisplacement(1);
                                    LocationServices.FusedLocationApi.requestLocationUpdates(client, request, new LocationListener() {
                                        @Override
                                        public void onLocationChanged(Location location) {
                                            LatLng myLatLong = new LatLng(location.getLatitude(),
                                                    location.getLongitude());
                                            CameraPosition cameraPosition = new CameraPosition.Builder().target(myLatLong).zoom(17).build();
                                            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                                        }
                                    });
                                } catch (SecurityException ex) {
    
                                }
                            }
                        }
    
                        @Override
                        public void onConnectionSuspended(int i) {
    
                        }
                    }).build();
            client.connect();
        }