当前位置: 首页 > 知识库问答 >
问题:

我如何在标记中显示地点的名称?

步德宇
2023-03-14

我在试着在Marker里显示那个地方的名字。我正在应用某人的代码,并想改变价格到名称。

我试着改变一些台词,但结果不是我想要的。

>

  • MarkerFromlatLong2.java

     public class MarkerFromLatLong2 extends FragmentActivity implements   OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleMap.OnMapClickListener {
    
    
    String myJSON;
    public static final String TAG_RESULTS = "result";
    public static final String TAG_LAT = "lat";
    public static final String TAG_LON = "lon";
    String lat;
    String lon;
    String price;
    char a, b, c, d, e;
    
    JSONArray reservedtimes = null;
    
    ArrayList<HashMap<String, String>> latlonList;
    
    Marker selectedMarker;
    View marker_root_view;
    TextView tv_marker;
    public GoogleMap mMap;
    final Context context = this;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    
        lat = new String();
        lon = new String();
        myJSON = new String();
        latlonList = new ArrayList<HashMap<String, String>>();
        getData("http://MY ADDRESS/getdata.php");
    }
    
    public void getData(String url) {
        class GetDataJSON extends AsyncTask<String, Void, String> {
    
            @Override
            public String doInBackground(String... params) {
    
                String uri = params[0];
    
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(uri);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
    
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    
                    String json;
                    while ((json = bufferedReader.readLine()) != null) {
                        sb.append(json + "\n");
                    }
    
                    return sb.toString().trim();
    
                } catch (Exception e) {
                    return null;
                }
            }
    
            @Override
            public void onPostExecute(String result) {
                myJSON = result;
                showList();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute(url);
    }
    
    
    public void showList() {
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            reservedtimes = jsonObj.getJSONArray(TAG_RESULTS);
    
            for (int i = 0; i < reservedtimes.length(); i++) {
                JSONObject c = reservedtimes.getJSONObject(i);
                lat = c.getString(TAG_LAT);
                lon = c.getString(TAG_LON);
    
                HashMap<String, String> location = new HashMap<>();
    
                location.put(TAG_LAT, lat);
                location.put(TAG_LON, lon);
    
                latlonList.add(location);
    
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }//여기까지
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.546167, 126.964673), 16));
        mMap.setOnMarkerClickListener(this);
        mMap.setOnMapClickListener(this);
    
        setCustomMarkerView();
        getSampleMarkerItems();
    }
    
    
    public void setCustomMarkerView() {
        marker_root_view = LayoutInflater.from(this).inflate(R.layout.marker_layout, null);
        tv_marker = (TextView) marker_root_view.findViewById(R.id.tv_marker);
    }
    
    public void getSampleMarkerItems() {
        ArrayList<MarkerItem> sampleList = new ArrayList();
    
        /*sampleList.add(new MarkerItem(Double.valueOf(lat), Double.valueOf(lon), 1906));
    
        sampleList.add(new MarkerItem(37.546167, 126.96568, 1906));
        sampleList.add(new MarkerItem(37.538523, 126.96568, 2500000));
        sampleList.add(new MarkerItem(37.527523, 126.96568, 100000));
        sampleList.add(new MarkerItem(37.549523, 126.96568, 15000));
        sampleList.add(new MarkerItem(37.538523, 126.95768, 5000));*/
    
        Intent intent = getIntent();
    
        Double latitude = intent.getDoubleExtra("lat", 000.000000D);
        Double longitude = intent.getDoubleExtra("lon", 000.000000D);
        Character price = intent.getCharExtra("price", (char) 123);
    
        sampleList.add(new MarkerItem(latitude, longitude, price));
    
        //sampleList.add(new MarkerItem(37.546167, 126.964673, 1906));
        sampleList.add(new MarkerItem(37.538523, 126.96568, b));
        sampleList.add(new MarkerItem(37.527523, 126.96568, c));
        sampleList.add(new MarkerItem(37.549523, 126.96568, d));
        sampleList.add(new MarkerItem(37.538523, 126.95768, e));
    
        for (MarkerItem markerItem : sampleList) {
            addMarker(markerItem, false);
        }
    }
    //get the lat and lon in order
    //
    
    
    public Marker addMarker(MarkerItem markerItem, boolean isSelectedMarker) {
    
        LatLng position = new LatLng(markerItem.getLat(), markerItem.getLon());
    
        char price = markerItem.getPrice();
        //String formatted = NumberFormat.getInstance().format((price)); //only shows 0(zero)
        //String formatted = String.format(String.valueOf(price)); //shows nothing(blank) in marker
        String formatted = NumberFormat.getInstance().format((price)); //only shows 0(zero)
    
        tv_marker.setText(formatted);
    
        if (isSelectedMarker) {
            tv_marker.setBackgroundResource(R.drawable.ic_marker_phone_blue);
            tv_marker.setTextColor(Color.WHITE);
        } else {
            tv_marker.setBackgroundResource(R.drawable.ic_marker_phone);
            tv_marker.setTextColor(Color.BLACK);
        }
    
        MarkerOptions markerOptions = new MarkerOptions();
        //markerOptions.title(Integer.toString(price)); // If I delete or change the code, the app stops after I click the marker.
        markerOptions.title(Integer.toString(price)); // If I delete or change the code, the app stops after I click the marker.
        markerOptions.position(position);
        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(this, marker_root_view)));
    
        return mMap.addMarker(markerOptions);
    
    }
    
    
    // change the View to Bitmap
    public Bitmap createDrawableFromView(Context context, View view) {
    
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
    
        return bitmap;
    }
    
    public Marker addMarker(Marker marker, boolean isSelectedMarker) {
        double lat = marker.getPosition().latitude;
        double lon = marker.getPosition().longitude;
        char price = (char) Integer.parseInt(marker.getTitle());
        MarkerItem temp = new MarkerItem(lat, lon, price);
        return addMarker(temp, isSelectedMarker);
    
    }
    
    @Override
    public boolean onMarkerClick(Marker marker) { //If I click a marker, move my center position to equal the marker's position.
    
        CameraUpdate center = CameraUpdateFactory.newLatLng(marker.getPosition());
        mMap.animateCamera(center);
    
        changeSelectedMarker(marker);
    
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle("SP #1")
                .setMessage("Want to reserve?\n")
                .setCancelable(false)
                .setPositiveButton("Reserve",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //Moving to the reservation screen
                                Intent intent = new Intent(MarkerFromLatLong2.this, ReserveActivity.class);
                                startActivity(intent);
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //cancel the dialog
                                dialog.cancel();
                            }
                        });
        //make a dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
        return true;
    }
    
    public void changeSelectedMarker(Marker marker) {
        // return the marker which was selected by user
        if (selectedMarker != null) {
            addMarker(selectedMarker, false);
            selectedMarker.remove();
        }
    
        // point the marker selected
        if (marker != null) {
            selectedMarker = addMarker(marker, true);
            marker.remove();
        }
    }
    
    @Override
    public void onMapClick(LatLng latLng) {
        changeSelectedMarker(null);
    }
    } //If user clicks the map where the marker doesn't exist, the map turns back to the starting status.
    
    public class MarkerItem {
     double lat;
    double lon;
    char price;
    
    public MarkerItem(double lat, double lon, char price) {
        this.lat = lat;
        this.lon = lon;
        this.price = price;
    }
    
    public double getLat() {
        return lat;
    }
    
    public void setLat(double lat) {
        this.lat = lat;
    }
    
    public double getLon() {
        return lon;
    }
    
    public void setLon(double lon) {
        this.lon = lon;
    }
    
    public char getPrice() {
        return price;
    }
    
    public void setPrice(char price) {
        this.price = price;
    }
    
    
     }
    
  • 共有1个答案

    杨安歌
    2023-03-14

    我想这会对你有用的链接。您可以简单地实现接口Google.InfoWindowAdapter,该接口具有名为getInfoContents(Marker Marker)的方法。在这个方法中,膨胀布局,在其中你要放关于所选标记的信息。

    我在很多项目中都用过这个。

    p.s.对不起,英语不好

     类似资料:
    • 问题内容: 我通过以下方式向GoogleMap添加默认标记: 如何使标记始终显示标题和摘要而无需触摸?我也想禁用隐藏它们。 问题答案: 这很简单:

    • 问题内容: 我想在Google Maps android上显示带有多个标记的位置,问题是当我运行我的应用程序时,它只显示一个位置/标记,这是我的代码: 我不知道我的错在哪里,希望有人能帮助我解决我的问题,非常感谢你 问题答案: 用这个: 或者,您也可以使用以下命令: 其中 纬度 和 经度 是存储在数组列表中的具有不同名称的字符串.....

    • 我有一堆标记,我可以在其中加载到谷歌地图上。我的问题是,相机不在标记之上。我可以将它集中在我所有的标记之上吗? 我正在使用以下代码添加标记: 我在堆栈上看了看,发现了这个问题,和我的问题一样: Android谷歌地图API V2中心标记 但它背后没有太多的背景,也不知道如何实现它。

    • 我在学Android。现在我正在开发一个使用地图API的应用程序。到目前为止,我做了以下操作:-我长时间点击了地图,并打开了一个新的ActivityCyclickedMap,其中显示了我点击的位置的lat和lon-我点击了save按钮,并将lat和lon添加到服务器和我的本地数据库中 我想做的事情是这样的:-当我打开地图时,我想让它显示一个标记,使用数据库中的lat和lon

    • 如何显示中心半径内的标记?我需要显示基于半径的位置,因为这将是我根据一个位置的km半径来查找此类位置的函数的基础。 正如你所看到的,我有许多记号笔。这是通过调用axios请求从我的数据库得到的。 这是我的代码片段

    • 问题内容: 我试图使用属性在模板内插入HTML 。但是由于某种原因,它不起作用。 我的代码: 我看不到HTML。如果更改为,则显示HTML,但在属性中显示如下内容: 问题答案: 确定,我找到了解决方案: JS: HTML: