【问题标题】:find area of polygon in google map v2 in android在android中的google map v2中查找多边形区域
【发布时间】:2015-02-25 11:33:46
【问题描述】:

我制作了一个 android 应用程序,显示谷歌地图,在该地图上我正在绘制一个多边形,其 LatLng 存储在 arrayList 中。我想找到在谷歌地图上绘制的那个多边形的区域,我试过了我尽力了,但没有成功,我无法使用 Spherical util,compute area 函数,因为它会报错,请给我最好的帮助,,,

提前谢谢,我在下面提供我的代码,请建议如何找到多边形的区域,正在绘制...谢谢..

dpackage com.example.mapss;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.google.android.gms.maps.CameraUpdateFactory;
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.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MainActivity extends Activity {


GoogleMap map;
ArrayList<LatLng> arrayPoints =null;
PolylineOptions polylineOptions;
PolygonOptions polygonOptions;
RadioGroup rg_views;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayPoints = new ArrayList<LatLng>();

    map=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.getUiSettings().setZoomControlsEnabled(true);
    map.getUiSettings().setCompassEnabled(true);
    map.getUiSettings().setMyLocationButtonEnabled(true);

    map.setMyLocationEnabled(true);

    rg_views =(RadioGroup)findViewById(R.id.rg_views);
    rg_views.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            if (checkedId==R.id.rb_map){
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

            }
            else if(checkedId==R.id.rb_satellite){
                map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

            }
            else if(checkedId==R.id.rb_terrain){
                map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            }

        }
    });

    map.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub

            MarkerOptions markerOptions = new MarkerOptions();

            markerOptions.position(point);

            markerOptions.title(point.latitude + ": " + point.longitude);

            map.clear();

            map.animateCamera(CameraUpdateFactory.newLatLng(point));


            map.addMarker(markerOptions);

            polygonOptions = new PolygonOptions();
            polygonOptions.fillColor(Color.RED);
            polygonOptions.strokeWidth(5);
            arrayPoints.add(point);
            polygonOptions.addAll(arrayPoints);
            map.addPolygon(polygonOptions);



        }
    });

    map.setOnMapLongClickListener(new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng point) {
            // TODO Auto-generated method stub

            map.clear();
            arrayPoints.clear();

        }
    });

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

【问题讨论】:

    标签: android maps


    【解决方案1】:

    Google Map Api 支持库与我配合良好 https://developers.google.com/maps/documentation/android/utility/

    【讨论】:

    • 这没有回答问题。请更仔细地阅读问题。问题不是如何判断一个点是否在多边形内,而是如何计算多边形的面积。
    • Tim 是对的,我想知道如何计算在地图上绘制的多边形的面积...
    【解决方案2】:

    你应该使用这个功能

    com.google.maps.android.SphericalUtil;
    public static double computeArea(java.util.List<LatLng> path)
    

    path 必须是封闭路径。 不要忘记在你的 gradle 中添加依赖项。

    compile 'com.google.maps.android:android-maps-utils:0.5+'
    

    查看此链接了解更多功能。 http://googlemaps.github.io/android-maps-utils/

    【讨论】:

      【解决方案3】:

      要使用computeArea方法,你需要导入

      com.google.maps.android.SphericalUtil
      

      就我而言,我手动导入了它。 然后就可以调用了

      SphericalUtil.computeArea(listOfLatLng)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-01
        相关资源
        最近更新 更多