【问题标题】:How to display marker on given Latitude and longitude values using google map in android?如何在android中使用谷歌地图在给定的纬度和经度值上显示标记?
【发布时间】:2016-11-16 05:44:33
【问题描述】:

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

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

//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.............................");
    }
}

【问题讨论】:

  • 在地图第二次变空白时检查您的日志是否有错误并将其发布在此处以便我们提供更好的帮助。
  • logcat 只显示地图 api 链接它们没有错误

标签: android google-maps


【解决方案1】:

用于静态显示标记或给出纬度和经度

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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 2014-04-30
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2016-01-29
    相关资源
    最近更新 更多