根据用户输入在 Google Maps V2 上绘制圆

Plot Circle On Google Maps V2 Based On User Input

我想根据用户输入的参数在地图上画一个圆

例如,如果用户输入1000 半径的参数。接下来发生的事情是,当单击按钮 "Plot" 时,它将打开 Google 地图并在其上绘制一个圆圈。

我已尝试 googled 寻找相关教程。不幸的是,所有人都在教如何点击地图并自动绘制形状,其中半径已在源与我的中定义。

以下是我的部分代码。

mapping.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#607D8B">

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Radius"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_span="1" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal"
                android:gravity="center"/>
        <requestFocus />

    </TableRow>



    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/buttonBarButtonStyle"
            android:text="Clear"
            android:layout_margin="10dp"
            android:background="#37474F"
            android:layout_column="0" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/button1"
            style="?android:attr/buttonBarButtonStyle"
            android:text="Plot"
            android:layout_margin="10dp"
            android:background="#37474F"/>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    </TableRow>

</TableLayout>

这是我的 java 文件。

package com.epsilon.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LossActivity extends Activity implements OnClickListener {

    EditText radius = null;
    Button plotButton = null;
    Button clearButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapping);

        radius = (EditText)findViewById(R.id.editText1);            
        plotButton = (Button)findViewById(R.id.button2);
        plotButton.setOnClickListener(this);
        clearButton = (Button)findViewById(R.id.button1);
        clearButton.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == plotButton) {
        double radius = Double.parseDouble(radius.getText().toString());
        Intent intent = new Intent(this,MapActivity.class);
        //something to write overhere
        intent.putExtra("radius",radius);// playing with this
        this.startActivity(intent);                     
        }
        else if(v == clearButton) {
            ((EditText)findViewById(R.id.editText1)).setText("");               
        }
    }

}

最后是 google 地图中的地图 activity,我认为问题是我不太清楚如何将上面的 java 文件与另一个 java 这个文件。

package com.epsilon.map;

import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class MapActivity extends FragmentActivity implements LocationListener{

    GoogleMap googleMap;

    Marker marker;
    Circle shape;

    private double radius;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        SupportMapFragment mfrag = (SupportMapFragment)    getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = mfrag.getMap();

        googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_map, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    private void setMarker(String locality, String country, double lat,double lng){

        LatLng LL = new LatLng(lat,lng);
        MarkerOptions options = new MarkerOptions()
                .title(locality)
                .position(LL)
                //.icon(BitmapDescriptor.fromResource(R.drawable.ic_mapmarker))
                .icon(BitmapDescriptorFactory.defaultMarker());

        marker = googleMap.addMarker(options);

        shape = drawCircle(LL);


    }

    private Circle drawCircle(LatLng LL) {

        CircleOptions options = new CircleOptions()
                .center(LL)
                .radius(radius)
                .fillColor(0x330000FF)
                .strokeColor(Color.BLUE)
                .strokeWidth(3);
        return googleMap.addCircle(options);
    }

    private void removeShape(){
        marker.remove();
        marker = null;
        shape.remove();
        shape = null;
    }

}

好的,所以你需要做的是这样的:

在你的LossActivity中:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v == plotButton) {
        double radius = Double.parseDouble(radius.getText().toString());
        Intent intent = new Intent(this, MapActivity.class);
        //something to write overhere
        Bundle b = new Bundle();
        b.putDouble("radius", radius);
        intent.putExtras(b);
        this.startActivity(intent);                     
        }
        else if(v == clearButton) {
            ((EditText)findViewById(R.id.editText1)).setText("");               
        }
    }
    .....

然后在你的 MapActivity:

private String provider;
private Location mCurrentLocation;
private LocationManager locationManager;
private double radius;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        Bundle b = getIntent().getExtras();
        if (b != null) {
            if (b.containsKey("radius")) {
                 radius = b.getDouble("radius");
            }
        }
        SupportMapFragment mfrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = mfrag.getMap();

        googleMap.setMyLocationEnabled(true);
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        // Here starts the old code
        //Criteria criteria = new Criteria();
        //provider = locationManager.getBestProvider(criteria, false);
        //locationManager.requestLocationUpdates(provider, 5000, 5, this);
        //mCurrentLocation = locationManager.getLastKnownLocation(provider);
        //if (mCurrentLocation != null) {
        //      onLocationChanged(mCurrentLocation);
        //}         
    }

    // Part of the new code
    @Override
    protected void onResume() {
        super.onResume();
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        locationManager.requestLocationUpdates(provider, 500, 0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
          setMarker("Locality", "Country", location.getLatitude(), location.getLongitude();
          locationManager.removeUpdates(this);
    }

此外,打开 GPS 以获得更好的准确性。