How to find specific point coordinates

Asked

Viewed 409 times

1

I need the coordinates of a specific point in Google Maps. Kind of like I click on a point and somehow the API shows me the string of mnsm. Because I need to calculate distance between 2 points

2 answers

2


Here are shown examples in HTML page and Android application.

  1. HTML page

    The following example opens a popup when you click on the map:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            window.onload = function () {
                var mapOptions = {
                    center: new google.maps.LatLng(18.9300, 72.8200),
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var infoWindow = new google.maps.InfoWindow();
                var latlngbounds = new google.maps.LatLngBounds();
                var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
                google.maps.event.addListener(map, 'click', function (e) {
                    alert("Latitude: " + e.latLng.lat() + "\r\nLongitude: " + e.latLng.lng());
                });
            }
        </script>
        <div id="dvMap" style="width: 500px; height: 500px">
        </div>
    </body>
    </html>
    

    See the Jsfiddle working.

  2. Android

    See the example to serguir for android:

    Class Eventdemoactivity:

    public class EventsDemoActivity extends FragmentActivity
       implements OnMapClickListener, OnMapLongClickListener {
    
          private GoogleMap mMap;
          private TextView mTapTextView;
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.events_demo);
    
              mTapTextView = (TextView) findViewById(R.id.tap_text);
    
              setUpMapIfNeeded();
          }
    
          private void setUpMap() //If the setUpMapIfNeeded(); is needed then...
          {
              mMap.setOnMapClickListener(this);
              mMap.setOnMapLongClickListener(this);
          }
    
          @Override
          public void onMapClick(LatLng point) {
              mTapTextView.setText("tapped, point=" + point);
          }
    
          @Override
          public void onMapLongClick(LatLng point) {
              mTapTextView.setText("long pressed, point=" + point);
          }
    }
    

    Layout events_demo.xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
      <TextView
        android:id="@+id/tap_text"
        android:text="@string/tap_instructions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
      <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>
    </LinearLayout>
    

    You can download the full example here.

  • Ooops. I forgot to say q is for android kkkkkkkkkk

  • I’ll add an example for Android

  • But, man, I got to the spot I wanted!! Now I can calculate the distance between this point and another that will vary according to the user’s location. Use the Location class.

  • Oh yeah, I’m gonna use, vlw man!!!!

0

Code for calculation of distance between 2 points:

public double distanciaEmMetros(double lat1, double long1, double lat2, double long2) {
    final Location start = new Location("Start Point");
    start.setLatitude(lat1);
    start.setLongitude(long1); //

    final Location finish = new Location("Finish Point");
    finish.setLatitude(lat2);
    finish.setLongitude(long2);

    final float distance = start.distanceTo(finish);

    return distance;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.