如何在静态视图的中心点之间画一条线到谷歌地图标记?

时间:2017-02-23 08:07:07

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

我的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()。任何帮助将不胜感激。

0 个答案:

没有答案