突出显示Google Maps v2中的区域

时间:2013-09-18 12:18:14

标签: android google-maps google-maps-api-2

如何在Google Maps v2 Android上突出显示某个区域/区域?我还列出了该特定地区的地理位置。滚动到该区域时,需要突出显示该区域。它需要填充附近区域的颜色以区分彼此,因此每个区域应该具有不同的颜色。

2 个答案:

答案 0 :(得分:3)

您需要通过选择地图上的某些点来绘制多边形。这是代码。

MainActivity.java

public class MainActivity extends FragmentActivity implements
    OnMapClickListener, 
    OnMapLongClickListener, 
    OnMarkerClickListener {

    private GoogleMap myMap;
    Location myLocation;
    boolean markerClicked;
    PolygonOptions polygonOptions;
    Polygon polygon;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) manager
            .findFragmentById(R.id.map);
        myMap = mapFragment.getMap();
        myMap.setMyLocationEnabled(true);
        myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        myMap.setOnMapClickListener(this);
        myMap.setOnMapLongClickListener(this);
        myMap.setOnMarkerClickListener(this);
        markerClicked = false;
    }

    @Override
    public void onMapLongClick(LatLng point) 
    {
        myMap.addMarker(new MarkerOptions()
            .position(point).title(point.toString()));

        markerClicked = false;
    }

    @Override
    public boolean onMarkerClick(Marker marker) 
    {

        if(markerClicked)
        {
            if(polygon != null)
            {
                polygon.remove();
                polygon = null;
            }

            polygonOptions.add(marker.getPosition());
            polygonOptions.strokeColor(Color.BLACK);
            polygonOptions.strokeWidth(5);
            polygonOptions.fillColor(0x884d4d4d);

            polygon = myMap.addPolygon(polygonOptions);
            marker.remove();

        }
        else    
        {
            if(polygon != null)
            {
                polygon.remove();
                polygon = null;
            }

            polygonOptions = new PolygonOptions().add(marker.getPosition());
            markerClicked = true;
            marker.remove();
        }

        return true;
    }

    @Override
    public void onMapClick(LatLng point) 
    {
        Toast.makeText(getApplicationContext(), 
            "Long Press to select locations", Toast.LENGTH_LONG).show();
    }
}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity" >


 <fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />


</RelativeLayout>

manfiest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.polygononmap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

     <permission
        android:name="com.example.googlemapwithsession.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="google.map.ver2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.polygononmap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your api key" />
    </application>

</manifest>

现在使用长按来选择地图上的点,然后点击标记会相应地绘制多边形..我觉得这会有所帮助。

答案 1 :(得分:1)

您只需在地图上绘制Polygon并用您需要的颜色填充它。