how to change the color of a particular area on Google map api v2 in android

醉酒当歌 提交于 2019-11-29 06:55:40

Like MaciejGórski said you have to use polygon with your Map's Onclick event.So i spent some time for you and came up with a solution.Now i am only drawing the polygon after 3 points added you can modify this to satisfy your needs.and also change the Color(Use RGBA color for highlighting the area inside the polygone).

package com.mzubair.mapkey;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

public class MainActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener {

private GoogleMap googleMap;
private TextView tapTextView;
private PolygonOptions polygonOptions;
private Polygon polygon;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tapTextView = (TextView) findViewById(R.id.textView1);
    polygonOptions = new PolygonOptions();


    // Getting reference to the SupportMapFragment of activity_main.xml
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);

                // Getting GoogleMap object from the fragment
                googleMap = fm.getMap();

                setUpMap();

}

 private void setUpMap() //If the setUpMapIfNeeded(); is needed then...
    {
        googleMap.setOnMapClickListener((OnMapClickListener) this);
        googleMap.setOnMapLongClickListener((OnMapLongClickListener) this);
    }

 @Override
    public void onMapClick(LatLng point) {
        tapTextView.setText("tapped, point=" + point);
        polygonOptions.add(point);
        countPolygonPoints();
    }

    @Override
    public void onMapLongClick(LatLng point) {
        tapTextView.setText("long pressed, point=" + point);
    }
    public void countPolygonPoints(){
        if(polygonOptions.getPoints().size()>3){



            polygonOptions.strokeColor(Color.RED);
            polygonOptions.strokeWidth((float) 0.30);
            polygonOptions.fillColor(Color.BLUE);
            polygon = googleMap.addPolygon(polygonOptions);

        }
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Here is the result after using this code.

Read the Detailed Post and download the Demo App Here

You need to gather all the points that create this shape as a List of LatLngs and use Polygon.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!