Retrieve first two digits from google v3 API response - javascript

I'm getting "sick" with in a simply problem. I'm using OpenLayers to display a map with a google map base layer. As an user suggestion i'm doing a request to the google geocode API with an address provided by the user.
The problem:
The google api response is like that:
lat: "-34.8836111"
lon: "-56.1819444"
After that i have to create a LonLat OpenLayers object and make a transformation, like that:
var lonLat = new OpenLayers.LonLat(position.coords.longitude,
position.coords.latitude)
.transform(
new OpenLayers.Projection("EPSG:4326"), //transform from WGS 1984
window.user_map.getProjectionObject() //to Spherical Mercator Projection
);
The problem is that lonLat format is in a different precission.
lat: -4148075.5841099103
lon: -6254145.441513423
I need to have only the first two integers digits that allow that displayable using the google map API (in another page).
Any help? This could be solved in ruby for example in a bebore_save method that formats the coordinates, or in the client side with javascript/jquery
Regards.

Convert numbers to a string:
var myStringLat = 123456.123654 + "";
Then get first two digits:
var myStringLat2Digits = myStringLat.substr(1, 2);
Then convert back to numbers:
var myNumberLat2Digits = Number(myStringlat2Digits, 10);

The problem was solved with a inverse order transformation before showing the second google map.
Like that:
var lonLat = new OpenLayers.LonLat(position.coords.longitude,
position.coords.latitude)
.transform(
window.user_map.getProjectionObject(), //transform from WGS 1984
new OpenLayers.Projection("EPSG:4326") //to Spherical Mercator Projection
);

Related

Why these openlayers' feaures get wrong coordinates?

I have a bunch of markers stored in a mysql database in a table with these attributes id,longitude,latitude.
With an ajax query I get these rows and print to console and they're exactly equal to the ones in the db.
When I create my features with openlayers, with the longitude and latitude retrieved from the db and stored in javascript variables, I don't understand why the markers are placed to another place (they're supposed to be in Italy and they are under Africa).
The interesting thing is that if I manually insert the coordinates while creating the features they show in the right place.
Browsing the internet and trying to debug my code I found some clues.
First, it may be cause of how I store longitude and latitude in the db. I saw there's someone who says DECIMAL(10,8) for latitude and DECIMAL(11,8) for longitude but all of my coordinates are similar to lon: 9.728068 and lat: 44.106414 so i decided to use DECIMAL(7,6) for longitude and DECIMAL(8,6) for latitude.
Second, if I try to console.log the data retrieved from the db I get the right coordinates e.g. 9.728068 and 44.106414 but if I get the coordinates from the features after I created them I get different values.
For the manually inserted coordinates, I get the exact same values, but for the other one no.
Here's a snippet of my code. It's a for loop that in this case will cicle 2 times
var lon = r[i]["Sensor_longitude"]; //getting longitude from success response
var lat = r[i]["Sensor_latitude"]; //getting latitude from success response
console.log(lon); //first time 9.728068, second time 9.728368
console.log(lat); //first and second time 44.106414
//using retrieved coordinates to create the feature
var areaFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([lon,lat], 'EPSG:4326', 'EPSG:3857')),
name: sniffer_name
});
//trying to put manually the coordinates
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([9.728068,44.106414], 'EPSG:4326', 'EPSG:3857')),
name: sniffer_name
});
console.log(ol.proj.transform(areaFeature.getGeometry().getCoordinates(),'EPSG:3857','EPSG:4326'));
console.log(ol.proj.transform(iconFeature.getGeometry().getCoordinates(),'EPSG:3857','EPSG:4326'));
I have 2 entries in my db which have
lon:9.728068, lat:44.106414
lon:9.728368 ,lat:44.106414
so I will make 4 markers (areaFeature and iconFeature for each entry). areaFeature has the coordinates retrieved from db and iconFeature has coordinates inserted manually.
I expected the first half of logs to be 9.728068, 44.106414 and the second one to be 9.728368, 44.106414 but instead I get this:
9.728068 //correct
44.106414 //correct
[9.728068, -45.893585099999996] //[correct,wrong]
[9.728068, 44.106414] //[correct,correct]
9.728368 //correct
44.106414 //correct
[9.728367999999998, -45.893585099999996] //[wrong,wrong]
[9.728068, 44.106414] //[correct,correct]
As you can see latitude is nearly correct (don't know why it adds so many digits after the point) but it is negative and longitude in the first case is correct but in the second one it has other digits in addition.

ArcGIS initial map location jsapi

I am trying to use a customers ArcGIS system to render a map on a page using the ArcGIS Javascript API.
To define a map in examples it displays:
map = new Map("map",{
basemap: "topo",
center: [-117.19,34.05], // lon, lat
zoom: 13
});
however this is showing fixed co-ordinates.
I want to be able to hit the RESTAPI in the customers internal system with a land id and return and plot the co-ordinates.
Currently I am retrieving a Degree Minute Second latitude and longitude from the land but wondering if there is a simpler way then having to get these values as a string, split them into degrees minutes seconds and then making a calculation to get their X,Y values to plot into this function to define a map.
Essentially I am just trying to load a map in a location specified by the land id and then after load map layers on top, all through the the local systems RestAPI and the Javascript API.
Thanks,
This is all about spatial reference(i.e 102100, 4326 etc) of the map.
Whatever you will add above snippet code this will add a map with basemap (topology) and center of the map will be co-ordinates which you have provided.
if you want to add a point to map and it has different format of geometry then you can use project geometry operation to convert.
require([
"esri/tasks/ProjectParameters", ...
], function(ProjectParameters, ... ) {
var params = new ProjectParameters();
params.geometries = [point];
params.outSR = outSR;
params.transformation = transformation;
gsvc.project(params);
...
});
For Reference: https://developers.arcgis.com/javascript/jsapi/geometryservice-amd.html#project
Convert Degree minute second to decimal/Lat long:
function ConvertDegreeAngleToDecimal(degrees, minutes, seconds )
{
//Decimal degrees =
// whole number of degrees,
// plus minutes divided by 60,
// plus seconds divided by 3600
return degrees + (minutes/60) + (seconds/3600);
}
Hope this will help you.
Let me know if you need more clarifications!

openlayers create marker (feature with icon) from WKT

I have a point object WKT. Like this: POINT (25.04568 48.221548). Also I have an icon in my project folder.
My goal is to show on a map an icon that represents a feature. Can it be just a normal OpenLayers feature (if yes, then how can I define that it should represent and icon) or do I need to create an OpenLayers marker (somehow create LonLat from WKT)?
It is fairly easy to add an icon if you have the point.
Just view the javascript source of this page:
OpenLayers example markers page
OpenLayers examples page
An important part to remember is that if you use an icon you have to use .clone() on it if you need it to display more than once.
Code snippet from above example:
"...
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,0),icon));
var halfIcon = icon.clone();
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(0,45),halfIcon));
marker = new OpenLayers.Marker(new OpenLayers.LonLat(90,10),icon.clone());
..."
Something like this on your point object:
point.transform(
new OpenLayers.Projection("EPSG:900913"), //from
map.getProjectionObject() //to
);
Of course you need to know what your points projection is.
There are plenty of examples out there.
Projections and OpenLayers.Geometry.Point in openlayers
Spherical Mercator - OpenLayers Library Documentation

Vector data(point) is drawn in wrong place on map (OpenLayers)

I need to show a map (world map, used default OpenLayers WMS) and one point on it (with events like onhover, etc). Here is my code:
var options = {
projection: new OpenLayers.Projection("EPSG:900913"),
maxResolution: 6000
};
map = new OpenLayers.Map('map', options);
var wmsLayer = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0",
{layers: 'basic'}
);
var vectors = new OpenLayers.Layer.Vector("Vector Layer");
point = new OpenLayers.Geometry.Point(20.088844299316406, 51.8321709083475);
vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);
map.addLayers([wmsLayer, vectors]);
map.zoomToMaxExtent();
But this code locates the point is drawn not in correct place, but somewhere near Africa (that place lat and lon is 0, 0). Question: Why this happens and can I fix it? I just need to locate the point to the correct place. Paradox when I print this point in console then it shows that the point lan and lon are as needed (as defined). But it is still in the wrong place...
Your map is in 900913, and point in 4326 projection. You have to transform it from 4326 to 900913:
point = new OpenLayers.Geometry.Point(20.088844299316406, 51.8321709083475);
point.transform(
new OpenLayers.Projection("EPSG:4326"),
new OpenLayers.Projection("EPSG:900913")
);
Also, consider centering your map by feature. zoomToMaxExtent() centers map to 0, 0. Since you have set maxResolution to 6000, only small part of map is visible and feature may be out of map bounds.
map.zoomToMaxExtent();
map.setCenter([point.x, point.y]);
Seems that new OpenLayers.Feature.Vector() uses OpenLayers.Geometry but not an OpenLayers.Geometry.Point type. But I never done such kind of app. Just a suggestion which looks reasonable.

How to convert latitude and longitude (from gps methods) to a geometry.location Google maps object

I have to work out with some coordinate on google maps, and I need to convert a coordinate like 46.1241241, 11.223235 (get by GPS, trought position.coords) to an geometry.location Google Maps API 3 object.
How can I do it?
Here is how you create the coordinates:
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

Categories