I need the coordinates of a specific Google Maps point. Like as if I clicked on a point and somehow the API showed me the mnsm string. Well I need to calculate distance between 2 points
I need the coordinates of a specific Google Maps point. Like as if I clicked on a point and somehow the API showed me the mnsm string. Well I need to calculate distance between 2 points
Here are examples in HTML page and Android application.
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><scripttype="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 JsFiddle working.
Android
See the example to follow 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 sample here .
Code for distance calculation 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;
}