Android谷歌地图从api乘以标记

时间:2016-07-12 12:13:03

标签: android api google-maps retrofit

我从api获得汽车研讨会列表,点击其中一个选项列表后会提供一个谷歌地图,其中显示了本次研讨会的标记信息,我的问题是如何做到这一点不会关闭地图,我可以看到所有其他地方都标记了标记,而不仅仅是选择的标记。

如果您需要,请提供:http://gdetut.com/api/firms?salt=63926e380bdc96ef990d57898daeb71c&category_id=1

地图活动:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {

private static final String LOG_TAG = "myLogs";
Marker marker;
String nameOfPlace;
String categoryOfPlace;
String ratingOfPlace;
double lon;
double lat;


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


    Places places = (Places) getIntent().getExtras().getSerializable("key");
    nameOfPlace = places.getName();
    categoryOfPlace = places.getSubcategory_name();
    ratingOfPlace = places.getRating();
    lon = Double.parseDouble(places.getLon());
    lat = Double.parseDouble(places.getLat());

    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);


}


//               load the map and set marker
@Override
public void onMapReady(final GoogleMap map) {

    marker = map.addMarker(new MarkerOptions()
            .position(new LatLng(lat, lon))
            .title(nameOfPlace)
            .snippet(categoryOfPlace + "Рейтинг:" + ratingOfPlace));


    marker.showInfoWindow();

    map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {

            return null;
        }

//                         set view for marker info
        @Override
        public View getInfoContents(Marker marker) {
            View v = getLayoutInflater().inflate(R.layout.marker_info, null);


            TextView mN = (TextView) v.findViewById(R.id.marker_name_id);
            TextView mC = (TextView) v.findViewById(R.id.marker_category_id);
            TextView mR = (TextView) v.findViewById(R.id.marker_rating_id);
            if (mN != null) {
                mN.setText(nameOfPlace);
            }
            if (mC != null) {
                mC.setText(categoryOfPlace);
            }
            if (mR != null) {
                mR.setText("Рейтинг: " + ratingOfPlace);
            }


            return v;
        }
    });


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
//        zooming camera
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 10));
    map.animateCamera(CameraUpdateFactory.zoomTo(17), 2000, null);
    map.setMyLocationEnabled(true);
    Log.d(LOG_TAG, "map is load");


 }


}

地方课程:

public class Places implements Serializable {


String name;
String geometry_name;
String rating;
String subcategory_name;
String favorite;
String csv_image;
String lon;
String lat;

public Places(String name, String geometry_name, String rating, String subcategory_name, String favorite, String csv_image, String lon, String lat) {
    this.name = name;
    this.geometry_name = geometry_name;
    this.rating = rating;
    this.subcategory_name = subcategory_name;
    this.favorite = favorite;
    this.csv_image = csv_image;
    this.lon = lon;
    this.lat = lat;

}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public String getGeometry_name() {
    return geometry_name;
}

public void setGeometry_name(String geometry_name) {
    this.geometry_name = geometry_name;
}

public String getRating() {
    return rating;
}

public void setRating(String rating) {
    this.rating = rating;
}

public String getSubcategory_name() {
    return subcategory_name;
}

public void setSubcategory_name(String subcategory_name) {
    this.subcategory_name = subcategory_name;
}

public String getFavorite() {
    return favorite;
}

public void setFavorite(String favorite) {
    this.favorite = favorite;
}

public String getCsv_image() {
    return csv_image;
}

public void setCsv_image(String csv_image) {
    this.csv_image = csv_image;
}

public String getLon() {
    return lon;
}

public void setLon(String lon) {
    this.lon = lon;
}

public String getLat() {
    return lat;
}

public void setLat(String lat) {
    this.lat = lat;
 }
}

改造成功:

    //               get the data from Retrofit
    Retrofit.getPlaces(new Callback<List<Places>>() {
        @Override
        public void success(final List<Places> places, Response response) {


//                save data into internal storage
            FileOutputStream fos = null;
            try {
                fos = openFileOutput("countries_file", Context.MODE_PRIVATE);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(places);
                oos.close();
                Log.d(LOG_TAG, "сохранение данных во внутреннюю память");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

//                initialize the adapter
            listView.setAdapter(new MyAdapter(MainActivity.this, places));
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
                    Places plasez = places.get(position);
                    mIntent.putExtra("key", plasez);
                    startActivity(mIntent);
                }
            });


        }

0 个答案:

没有答案