【问题标题】:How to draw a line between static view's center points to google map marker?如何在静态视图的中心点与谷歌地图标记之间画一条线?
【发布时间】:2017-02-23 08:07:06
【问题描述】:

我的 xml 文件中有一个静态线性布局和一个谷歌地图。我想在线性布局的中心底部到标记位置之间画一条直线。我尝试在地图中使用折线,但它也在地图中的标记之间绘制。 下面是我的xml文件-

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/rel_m1"
        android:layout_above="@+id/bottom_ll"
        android:id="@+id/frameLayout">
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/map"
            tools:context="com.business.introslider.MapsActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:orientation="vertical"
            android:id="@+id/pickup_ll"
            android:onClick="OpenPickup"
            android:elevation="30dp"
            android:background="#ffffff">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pickup from"
                android:layout_marginTop="5dp"
                android:textSize="14sp"
                android:textColor="#555555"
                android:padding="5dp"
                android:drawableLeft="@drawable/s_green"
                android:drawablePadding="5dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pin location"
                android:layout_marginTop="5dp"
                android:textSize="16sp"
                android:textColor="#000000"
                android:padding="5dp"
                android:id="@+id/text_pickup_location"
                android:layout_marginBottom="5dp"/>
        </LinearLayout>
</FrameLayout>

我的java代码如下-

public void setCurrentLocation(){
        try {
            //to show my current location in the map

            mMap.setMyLocationEnabled(true);
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {
                    Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
                }
            });
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            //to request location updates
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
            ViewTreeObserver vto = pickup_ll.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        pickup_ll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        pickup_ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                    float x = pickup_ll.getX() + pickup_ll.getMeasuredWidth() / 2;
                    float y = pickup_ll.getY() + pickup_ll.getMeasuredHeight() / 2;

                //    float x= getWindowManager().getDefaultDisplay().getWidth()/2;
                    point1 = new Point(Math.round(x),Math.round(y));
                    Log.e(TAG, "point1===" + point1);
                    LatLng latLng1 = mMap.getProjection().fromScreenLocation(point1);
                    Log.e(TAG,"latLng1==="+latLng1);
                    points.add(latLng1);
                }

            });

 @Override
    public void onLocationChanged(Location location) {
        Log.e(TAG, "onlocation changed");
        //To clear map data
        mMap.clear();
        //To hold location
        if(latLng !=null) {
            Log.e(TAG, "latLng onlocation changed==" + latLng);
        }
        else {
            latLng = new LatLng(location.getLatitude(), location.getLongitude());
        }
        //to create a marker in the app
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("My Location");
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.color(Color.BLACK);
        polylineOptions.width(2);
        points.add(latLng);
        polylineOptions.addAll(points);
        mMap.addPolyline(polylineOptions);
        mMap.addMarker(markerOptions);

        //opening position with some zoom level in the app
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
    }

我在 googleApiClient 的 onMapReady 方法中调用了这个 setCurrentLocation()。任何帮助将不胜感激。

【问题讨论】:

    标签: android google-maps google-maps-markers


    【解决方案1】:

    您是否尝试过以这种方式绘制路径? https://github.com/hiepxuan2008/GoogleMapDirectionSimple/ 这将让您在两个地方之间绘制一条路径。确保更改 API 密钥。

    【讨论】:

    • 这个链接对我没有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2012-11-06
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2019-01-19
    相关资源
    最近更新 更多