在我的应用程序上,我将通过更多不同的json响应在谷歌地图上添加更多标记。 现在我使用Retrofit来调用终点,回复json响应并在谷歌地图上添加标记,这是我的代码:
private void getPlacemark(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example.com/api/v2.1")
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl("https://api.citybik.es/v2/networks/")
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<ResponsePlacemarks> call = service.getPlacemark();
APIService service1 = retrofit1.create(APIService.class);
Call<NetworkResponse> call1 = service.getNetwork();
call.enqueue(new Callback<ResponsePlacemarks>() {
@Override
public void onResponse(Response<ResponsePlacemarks> response, Retrofit retrofit) {
Log.d("risposta: ",new Gson().toJson(response));
try {
//mMap.clear();
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getPlacemarks().size(); i++) {
Double lat = response.body().getPlacemarks().get(i).getCoordinates().get(1);
Double lng = response.body().getPlacemarks().get(i).getCoordinates().get(0);
String placeName = response.body().getPlacemarks().get(i).getAddress();
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions.position(latLng);
// Adding Title to the Marker
markerOptions.title(placeName);
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.pin_green));
// Adding Marker to the Camera.
Marker m = mMap.addMarker(markerOptions);
//mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
}
});
call1.enqueue(new Callback<NetworkResponse>() {
@Override
public void onResponse(Response<NetworkResponse> response, Retrofit retrofit) {
//Log.d("risposta_bike: ",new Gson().toJson(response));
try {
//mMap.clear();
// This loop will go through all the results and add marker on each location.
for (int i = 0; i < response.body().getNetwork().getStations().size(); i++) {
Double lat = response.body().getNetwork().getStations().get(i).getLatitude();
Double lng = response.body().getNetwork().getStations().get(i).getLongitude();
int placeName = response.body().getNetwork().getStations().get(i).getFreeBikes();
MarkerOptions markerOptions1 = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions1.position(latLng);
// Adding Title to the Marker
markerOptions1.title("Bici disponibili: " + placeName);
markerOptions1.icon(BitmapDescriptorFactory.fromResource(R.mipmap.gray_pin));
// Adding Marker to the Camera.
Marker mm = mMap.addMarker(markerOptions1);
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
}
});
}
现在我只打电话给这个2终点,我在地图上有更多的标记,在胎儿中我将添加更多标记。 所以我想我可以改变我的代码,并在json resposnse添加所有标记,因为它们在屏幕上移动并移除标记,因为它们离开屏幕,显示当前在用户感知的GoogleMap可视范围内的标记,如这个程序:
以及如何仅在范围缩放中显示标记,例如(缩放&gt; 10)
这是我添加谷歌地图的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.menu1, container, false);
((MainActivity) getActivity()).setToolbarTitle("Mobility");
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
boolean success = mMap.setMapStyle(new MapStyleOptions(getResources()
.getString(R.string.style_json)));
getPlacemark();
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Location Permission already granted
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
任何帮助和想法? 感谢