我在地图片段中使用位置选择器编写了一个代码以显示附近的位置,但是它无法将相机直接移动到我当前的电话位置,我必须长时间用center = new 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);
}
}
答案 0 :(得分: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();
}