Android仅为屏幕在地图上添加标记
在我的应用程序中,我将通过更多不同的json响应在谷歌地图上添加更多标记。 现在我使用改型来调用端点,检索json响应并在google地图上添加标记,这是我的代码:Android仅为屏幕在地图上添加标记,android,google-maps,marker,Android,Google Maps,Marker,在我的应用程序中,我将通过更多不同的json响应在谷歌地图上添加更多标记。 现在我使用改型来调用端点,检索json响应并在google地图上添加标记,这是我的代码: private void getPlacemark(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/api/v2.1") .addConverterFactory(GsonC
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) {
}
});
}
有什么帮助和想法吗?
谢谢检查这可能有助于您了解群集,但我在帖子中提出的问题的解决方案与群集不同。我将只在地图上显示用户认为当前在谷歌地图可视范围内的标记检查这可能有助于您了解集群,但我在我的问题上发布的解决方案与集群不同。我将只在地图上显示用户认为当前在谷歌地图可视范围内的标记
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);
}
}