如何改变特定区域的颜色对Android的谷歌地图API第2版 [英] how to change the color of a particular area on Google map api v2 in android

查看:441
本文介绍了如何改变特定区域的颜色对Android的谷歌地图API第2版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,它使用谷歌地图API第2版,并希望以纪念一种特定的颜色上单击事件在地图上的特定区域。例如,当我点击印度,覆盖全国的区域应为绿色。

I am working on an Android app that makes use of the Google Maps API v2 and would like to mark a specific area on the map with a certain color on a click event. For example, when I click on India, the area covering the country should be colored green.

我已经使用 GroundOverlay ,但它需要的图像在地图上显示的东西,造成颜色的区域不是一个很好的匹配。因为两者的地图和图像有自己的形状,这没有覆盖的确切区域。

I'm already using GroundOverlay, but it requires an image to display something on the map, resulting in the colored area not being a good match. Since both the map and the image have their own shape, this does not cover the exact area.

谁能告诉我如何更准确的色彩是Android谷歌地图API第2版的地图?

Can anyone please tell me how to color an Android Google Maps API v2 map more accurately?

推荐答案

就像MaciejGórski说你要使用多边形您的地图的onClick event.So我花了一些时间,你想出了一个solution.Now我只绘制多边形后的3点加到你可以修改此,以满足您的needs.and也改变了颜色(使用RGBA颜色突出显示该区域的polygone内)。

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;
}

}

下面是使用此code之后的结果。

Here is the result after using this code.

阅读详细的发布和下载演示程序下面

Read the Detailed Post and download the Demo App Here

这篇关于如何改变特定区域的颜色对Android的谷歌地图API第2版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆