当前位置: 首页 > 知识库问答 >
问题:

如何在谷歌地图中标注经纬度?

蔡明贤
2023-03-14
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            double longitude = location.getLongitude();
            double latitude = location.getLatitude();



            for (int i = 0; i < 10; i++) {
                // random latitude and logitude
                double[] randomLocation = createRandLocation(latitude,
                        longitude);

                // Adding a marker
                MarkerOptions marker = new MarkerOptions().position(
                        new LatLng(randomLocation[0], randomLocation[1]))
                        .title("Hello Maps " + i);

                Log.e("Random", "> " + randomLocation[0] + ", "
                        + randomLocation[1]);



                if (i == 0)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                if (i == 1)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                if (i == 2)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
                if (i == 3)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                if (i == 4)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                if (i == 5)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
                if (i == 6)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED));
                if (i == 7)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
                if (i == 8)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
                if (i == 9)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
                googleMap.addMarker(marker);

                // Move the camera to last position with a zoom level
                if (i == 9) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(randomLocation[0],
                                    randomLocation[1])).zoom(15).build();
                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }


    private double[] createRandLocation(double latitude, double longitude) {
        return new double[] { latitude + ((Math.random() - 0.5) / 500),
                longitude + ((Math.random() - 0.5) / 500),
                150 + ((Math.random() - 0.5) * 10) };

    }
}

共有1个答案

宗政卓
2023-03-14

首先,用中心点创建,如下所示:

Circle circle = map.addCircle(new CircleOptions()
 .center(new LatLng(centerpointLat, cnterpointLong))
 .radius(10)
 .strokeColor(Color.RED)
 );

现在,查找从圆圈中心距离,并存储到一个数组ArrayList,如下所示:

float[] distances = new float[1];
final ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i=0;i<10;i++){

double[] randomLocation = createRandLocation(latitude,
                    longitude);

Location.distanceBetween(randomLocation[0],randomLocation[1],
            circle.getCenter().latitude, circle.getCenter().longitude, distances);
 arrayList.add((int) results[0]);
 }

现在,检查距离圆圈中心距离是否<10,类似:

  for(int i=0;i<10;i++){

  if(arrayList.get(i) < circle.getRadius() or (directly provide(circle radius))){
         //Add Marker belong here   
        } else {
         //Any Message over here  
        }
  } 
public class BasicMapActivity_new2 extends FragmentActivity implements
    LocationListener, OnMarkerClickListener {

private GoogleMap mMap;
private LocationManager locationManager;
private String provider;
Double Latitude, longitude;
Circle circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo);
    setHeaderBackground();

    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map2)).getMap();
    mMap.setMyLocationEnabled(true);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabledGPS = service
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean enabledWiFi = service
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if (!enabledGPS) {
        Toast.makeText(BasicMapActivity_new2.this, "GPS signal not found",
                Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    } else if (!enabledWiFi) {
        Toast.makeText(BasicMapActivity_new2.this,
                "Network signal not found", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);
    // getCurrentLocation();

    // Initialize the location fields
    if (location != null) {
        // Toast.makeText(BasicMapActivity_new.this, "Selected Provider " +
        // provider,
        // Toast.LENGTH_SHORT).show();
        onLocationChanged(location);
    } else {

        // do something
    }
    // setUpMapIfNeeded();
    setUpMap();
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
    setUpMapIfNeeded();
}


private void setUpMap() {
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setTiltGesturesEnabled(true);
    mMap.getUiSettings().setRotateGesturesEnabled(true);
    mMap.getUiSettings().setScrollGesturesEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setZoomGesturesEnabled(true);
    SMSLocation();
}

public void SMSLocation() {

    Circle circle = mMap.addCircle(new CircleOptions()
            .center(new LatLng(Latitude, longitude)).radius(100)
            .strokeColor(Color.RED));

    float[] distances = new float[1];
    final ArrayList<Integer> arrayList = new ArrayList<Integer>();

    for (int i = 0; i < 10; i++) {
        // random latitude and logitude
        double[] randomLocation = createRandLocation(Latitude, longitude);

        // Adding a marker
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(randomLocation[0], randomLocation[1])).title(
                "Hello Maps " + i);

        Location.distanceBetween(randomLocation[0], randomLocation[1],
                circle.getCenter().latitude, circle.getCenter().longitude,
                distances);

        // arrayList.add((int) distances[0]);
        System.out.println("latitude: " + randomLocation[0] + ", "
                + randomLocation[1]);

        Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);
        Toast.makeText(
                BasicMapActivity_new2.this,
                " Random Location " + randomLocation[0] + ","
                        + randomLocation[0], Toast.LENGTH_LONG).show();

        if (i == 0)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
        if (i == 1)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
        if (i == 2)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
        if (i == 3)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        if (i == 4)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        if (i == 5)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        if (i == 6)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED));
        if (i == 7)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
        if (i == 8)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
        if (i == 9)
            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

        if (distances[0] < circle.getRadius()) {
            // Add Marker belong here
            mMap.addMarker(marker);
        }
        // Move the camera to last position with a zoom level
        /*
         * if (i == 9) { CameraPosition cameraPosition = new
         * CameraPosition.Builder() .target(new LatLng(randomLocation[0],
         * randomLocation[1])).zoom(15).build();
         * mMap.animateCamera(CameraUpdateFactory
         * .newCameraPosition(cameraPosition)); }
         */
    }

}

@Override
public boolean onMarkerClick(Marker arg0) {
    // TODO Auto-generated method stub
    return false;
}

Marker startPerc = null;
Location old_one;

@Override
public void onLocationChanged(Location location) {

    // mMap.clear();

    double lat = location.getLatitude();
    double lng = location.getLongitude();
    // Toast.makeText(BasicMapActivity_new.this, "Location " + lat+","+lng,
    // Toast.LENGTH_LONG).show();
    LatLng coordinate = new LatLng(lat, lng);
    Latitude = lat;
    longitude = lng;

    Toast.makeText(BasicMapActivity_new2.this,
            "Location " + coordinate.latitude + "," + coordinate.longitude,
            Toast.LENGTH_LONG).show();

    startPerc = mMap.addMarker(new MarkerOptions()
            .position(coordinate)
            .title("Current Location")
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 18.0f));

}

private double[] createRandLocation(double latitude, double longitude) {
    return new double[] { latitude + ((Math.random() - 0.5) / 500),
            longitude + ((Math.random() - 0.5) / 500),
            150 + ((Math.random() - 0.5) * 10) };

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
    android:id="@+id/map2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 类似资料:
  • 长话短说,我想更新地图上的标记仅使用传感器数据,但我在API中找不到允许我更新地图上的纬度/经度的库函数。根据我计算的新纬度/经度,有什么东西可以做到这一点吗?

  • 我想更改Google Maps上选定的标记图标,因此我有以下代码: 在这一行,我从下面得到错误: 例外情况如下:

  • 我成功创建了一个活动,用户可以在其中发布他的状态并将他的当前位置放置在android工作室中创建的google map中。我还尝试在map中查看所有其他用户,我成功地做到了。但是,随着他们的位置发生变化,不断增加。我想实现这样一种情况,即用户在移动时可以在地图上看到彼此,并且这些也会相应地移动。我使用Firebase作为我的后端来存储他们的位置和状态。这是我想出的代码。 请帮帮我!提前感谢!:)

  • 我在我的flutter应用程序中使用google_maps_flutter来使用谷歌地图我有自定义标记图标,我用BitmapDescriptor.from字节(mark erIcon)加载它,但我想用一些文本显示Url的图标。 这就是我们要达到的目的 是否可以通过flutter或任何可以动态创建图像的外部图像依赖来实现相同的效果。

  • 所以我正在准备一个页面,其中有一个谷歌地图,有两个形式,即纬度和经度,和一个提交按钮。