如何在Android中使用谷歌地图在给定的纬度和经度值上显示标记?

时间:2016-11-16 05:44:33

标签: android google-maps

我正在使用谷歌地图开发一个应用程序。在这里我想显示谷歌地图提供的纬度和经度值。在应用程序我从数据库中获取纬度和经度值,并在此纬度和经度的值,我想在谷歌地图上显示标记。

以下是我使用的代码,在下面的代码中我显示了纬度和经度的获取值以及在谷歌地图上的纬度和经度的这个获取值上的标记显示但问题是当我运行应用程序第一次时间地图显示准确地获取纬度和经度的值但是当我第二次打开应用程序时它在地图上显示蓝色。我想以纬度和经度的获取值显示地图。我该怎么做?

//java code
public class Location_Track6 extends FragmentActivity {

    JSONArray result = null;

    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String CON = "CON";
    JSONObject jsonobject;
    JSONArray jsonarray;
    SupportMapFragment fm;

    private String url ="";

    private static final String TAG_USER = "result";

    //   private static final String TAG_SNAME = "pseats";
    private static final String TAG_LONG = "longitude";
    private static final String TAG_LAT = "latitude";
    private static final String TAG_ADDRESS = "paddress";

    private List<LatLng> points = new ArrayList<>();
    Polyline line; //added

    TextView tv_mobno, tv_latitude, tv_longitude, tv_time;
    String getLatitude;
    String getLongitude;
    Button slocation;

    TextView etLng, etLat;
    Button btnShow;

    double lati=0;
    double lngi=0;

    GoogleMap googleMap;

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

        tv_mobno=(TextView)findViewById(R.id.textView_mob);
        tv_latitude=(TextView)findViewById(R.id.textView_latitude);
        tv_longitude=(TextView)findViewById(R.id.textView_longitude);
        tv_time=(TextView)findViewById(R.id.textView_time);

        etLat = (TextView) findViewById(R.id.et_lat);
        etLng = (TextView) findViewById(R.id.et_lng);

        fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

        //    slocation=(Button)findViewById(R.id.button_slocation);

        url = "http://example.in/gmap_track.php";


// Getting reference to button btn_show
        btnShow = (Button) findViewById(R.id.btn_show);

        new JSONParse().execute();

// Setting click event listener for the button
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getUserLocation();


            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(Location_Track6.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
        @Override
        protected void onPostExecute(JSONObject json) {
            pDialog.dismiss();
            try {

                // Getting JSON Array
                result = json.getJSONArray(TAG_USER);
                JSONObject c = result.getJSONObject(0);


                // Storing  JSON item in a Variable
                String lat = c.getString(TAG_LAT);
                String lng = c.getString(TAG_LONG);


                //Set JSON Data in TextView

                etLat.setText(lat);
                etLng.setText(lng);


                lati = Double.parseDouble(etLat.getText().toString());
                lngi = Double.parseDouble(etLng.getText().toString());

                //    MarkerOptions marker = new MarkerOptions().position(new LatLng(lati, lng)).title("point");
                //    googleMap.addMarker(marker);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    private  void getUserLocation(){

        LatLng position = new LatLng(lati, lngi);

        // Instantiating MarkerOptions class
        MarkerOptions options = new MarkerOptions();

        // Setting position for the MarkerOptions
        options.position(position);

        // Setting title for the MarkerOptions
        options.title("Position");

        // Setting snippet for the MarkerOptions
        options.snippet("Latitude:"+lati+",Longitude:"+lngi);

        // Getting Reference to SupportMapFragment of activity_map.xml


        // Getting reference to google map
        googleMap = fm.getMap();

        // Adding Marker on the Google Map
        googleMap.addMarker(options);

        // Creating CameraUpdate object for position
        CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

        // Creating CameraUpdate object for zoom
        CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(13);

        // Updating the camera position to the user input latitude and longitude
        googleMap.moveCamera(updatePosition);

        // Applying zoom to the marker position
        googleMap.animateCamera(updateZoom);
    }

    private void addMarker() {
        MarkerOptions options = new MarkerOptions();

        // following four lines requires 'Google Maps Android API Utility Library'
        // https://developers.google.com/maps/documentation/android/utility/
        // I have used this to display the time as title for location markers
        // you can safely comment the following four lines but for this info
    /*    IconGenerator iconFactory = new IconGenerator(this);
        iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
        options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(String.valueOf(R.drawable.one))));
        options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
    */
        LatLng currentLatLng = new LatLng(lati,lngi);
        options.position(currentLatLng);
        Marker mapMarker = googleMap.addMarker(options);
        //    long atTime = mCurrentLocation.getTime();
        //    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
        //    mapMarker.setTitle(mLastUpdateTime);
        mapMarker.setTitle("point");
       // Log.d(TAG, "Marker added.............................");
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
                13));
       // Log.d(TAG, "Zoom done.............................");
    }
}

1 个答案:

答案 0 :(得分:0)

按静态显示标记或提供纬度和经度

lat = 22.368025;
lon =91.849106;
loc = new LatLng(lat, lon);
marker = googleMap.addMarker(new MarkerOptions().position(loc).title("Hello Chittagong").snippet("A nice city"));

用于更改标记颜色

marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));`

放大位置

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 11.0f));