Centering Google Maps depending on pins - google-maps

I have a ASP.NET website which contains a Google Map. I am using the GoogleMapForASPNet framework made by some clever and helpful individual.
Anywho, I have a problem with centering the map after inserting two pins.
Basically, I am calculating the middle point of the pins and setting that as the Center Point of the map.
Below is my code:
GooglePoint newPoint = new GooglePoint();
double newLat = 0;
double newLong = 0;
if (googlePointA.Latitude > googlePointB.Latitude)
{
newLat = googlePointA.Latitude - googlePointB.Latitude;
newPoint.Latitude = googlePointA.Latitude - newLat;
}
else
{
newLat = googlePointB.Latitude - googlePointA.Latitude;
newPoint.Latitude = googlePointB.Latitude + newLat;
}
if (googlePointA.Longitude > googlePointB.Longitude)
{
newLong = googlePointA.Longitude - googlePointB.Longitude;
newPoint.Longitude = googlePointA.Longitude - newLong;
}
else
{
newLong = googlePointB.Longitude - googlePointA.Longitude;
newPoint.Longitude = googlePointB.Longitude + newLong;
}
GoogleMapForASPNet1.GoogleMapObject.CenterPoint = newPoint;
GoogleMapForASPNet1.GoogleMapObject.ZoomLevel = 8;
It works half and half, but not properly. As in, when I feed different Pins, it doesn't really center the map, but close enough. Or sometimes, the other pin will be off the map but only an inch, which means the map isn't centered at all.
The zoom is static because the pins will always be close by, so there's no need for me to make it dynamic.
Any help is extremely, extremely, extremely appreciated.
Thank you.

The Google Maps API has an object to do that: google.maps.LatLngBounds
Just create a LatLngBounds object, add your pin coordinates to it with the extend() method, then use the map panToBounds(yourBounds) method and you're all set!

Related

Bing Maps Layers - Finding information on segment

I have a large object, full of WKT information from a GIS system. I'm looping over the data and mapping it into layers, then pushing those layers onto the map. This is working fine and I'm getting the right data showing up.
for (var j = 0; j < 10; j++) {
var dataLayer = new Microsoft.Maps.Layer($scope.thing);
for (var i = 0; i < bArray.length; i++) {
if (bArray[i].count == $scope.thing) {
dataLayer.add(new Microsoft.Maps.WellKnownText.read(bArray[i].wkt);
dataLayer.item = bArray[i].count;
}
}
Microsoft.Maps.Events.addHandler(dataLayer, "click", polylineClicked);
$scope.map.layers.insert(dataLayer);
$scope.map.layers[j].setVisible(false);
$scope.thing++;
}
The data in each layer breaks down into different categories, stored in the field "item", and I show those categories in a side legend.
My question is how do I find "item" for each segment on the map? When I view the map object I can see the layers, when I go into the layers I can see the primitives, but when I enter the primitive, they all have the same category in "item", instead of what they should have.
How do I find "item" for each segment?
Thanks
Docmur
First off, use the metadata property of the layer to store custom info, otherwise you risk overwriting one of the internal properties of the layer class. For example: dataLayer.metadata = { item: "custom data" };
That said, your item property is on the dataLayer, not on the individual primitives. It also looks like you are adding the same value to it over an over again on the inner loop so you will end up with a lot of values that are the same. Can you provide more details on what you want to achieve as there likely is a much cleaner way to do this.

Oxyplot showing HeatMapSeries behind ScatterSeries

I am trying to combine LineSeries, ScatterSeries and HeatmapSeries on a single OxyPlot instance. I am able to show the first two just fine on the same plot and it looks like the following:
The axes for this are generated by the following code:
var xAxis = new DateTimeAxis();
xAxis.Key = "X";
xAxis.Position = AxisPosition.Bottom;
xAxis.AbsoluteMinimum = DateTimeAxis.ToDouble(CurrentPass.AOS);
xAxis.AbsoluteMaximum = DateTimeAxis.ToDouble(CurrentPass.LOS);
xAxis.AxislineColor = xAxis.TextColor = xAxis.TicklineColor = xAxis.MajorGridlineColor = OxyColors.DarkGray;
var yAxis = new LinearAxis();
yAxis.Key = "Y";
yAxis.Position = AxisPosition.Left;
yAxis.AbsoluteMinimum = 0.0;
yAxis.AbsoluteMaximum = MaximumFrequency;
yAxis.Maximum = MaximumFrequency;
yAxis.AxislineColor = yAxis.TextColor = yAxis.TicklineColor = yAxis.MajorGridlineColor = OxyColors.DarkGray;
If I add in a third axes for the Heatmap (not even a HeatMapSeries yet), I get the following:
The extra code here for the axis for the HeatMap is:
var heatmapAxis = new LinearColorAxis();
heatmapAxis.AbsoluteMinimum = 0.0;
heatmapAxis.AbsoluteMaximum = MaximumFrequency;
heatmapAxis.Palette = OxyPalettes.Gray(1024);
heatmapAxis.Key = "HeatMap";
I am not sure what's going on here. The line series still shows. And all the ScatterPoints from the scatterseries I have are definitely there - the tracker shows up and I can interact with the points (hover, click etc.). But the points don't show. If I add a HeatMapSeries, the HeatMapSeries data does show up as expected, the LineSeries data still shows up but no ScatterSeries data.
Again, the HeatMap data and the Scatter Series data show up individually, but never together.
Has anyone encountered this before? Are there workarounds?
Thanks,
Aditya
Sorry this is a bit late but just stumbled upon this. I had something similar with a line not showing up on a Heatmap and after some experimenting i realized the order in which the series' are added to the plot model make a big difference. Make sure you add your ScatterSeries after you've added your Heatmap to the plot model.

Google map as background to my sharpmap map not aligning correctly

I've been at this for quite a while now.
What I'm trying to achieve here is to add Google map as background layer to my Sharpmap, I'm able to achieve this but the problem I'm facing now is My map always centre's to a point near a Greenland sea in Google Map , it's like it won't take my centre point coordinates.
I'm using Sharpmap 1.1 with BruTile 0.7.4.4
So far I've done the below.
SharpMap.Map _map = new SharpMap.Map();
SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("parcel");
SharpMap.Data.Providers.MsSqlSpatial DBlayer = new SharpMap.Data.Providers.MsSqlSpatial(_connectionString, "XXXXXX", "XXXX", "XXX");
layer.Style.Fill = new SolidBrush(Color.Transparent);
layer.Style.Outline = new Pen(Color.Black);
layer.Style.EnableOutline = true;
layer.MaxVisible = 13000;
layer.DataSource = DBlayer;
ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
layer.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
layer.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
//SharpMap.Layers.TileLayer layerBackground = new TileLayer(new BingTileSource(BingRequest.UrlBing, "", BingMapType.Aerial), "TileLayer");
SharpMap.Layers.TileLayer layerBackground = new TileLayer(new GoogleTileSource(GoogleMapType.GoogleMap), "googlemaps");
_map.Layers.Add(layerBackground);
_map.Layers.Add(layer);
_map.BackColor = Color.White;
//-98.526890,29.411539
_map.Center = new GeoAPI.Geometries.Coordinate(0,0);
return _map;
Even if I manually give Geo Coordinates It just points to the same spot in the sea.
Please see the below Google map Geo point, this is the point where my map shows as centre. Every time I generate no matter what I do it shows this point as its centre.
71.946088, -3.956171
Any help is much appreciated. Thanks and Cheers!
I found the problem for Google map layer not centring.
The reason is that I used QGIS to convert my ESRI shapefile from WGS84(EPSG:4326) format to Spherical Mercator(EPSG:900913) and it changed the coordinates format but
Google Maps use Google Maps Global Mercator (Spherical Mercator).
When I used an online converter to test this out which you can find here. When I gave the resulting coordinates, it worked out just fine. Now all I have to do is find a way to convert Google Maps Global Mercator (Spherical Mercator).
Thanks for your help #pauldendulk.
I was running into the same problem and in theirExamples they provide a LatLongToGoogle transformation function
public static ICoordinateTransformation LatLonToGoogle()
{
CoordinateSystemFactory csFac = new CoordinateSystemFactory();
CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
IGeographicCoordinateSystem sourceCs = csFac.CreateGeographicCoordinateSystem(
"WGS 84",
AngularUnit.Degrees,
HorizontalDatum.WGS84,
PrimeMeridian.Greenwich,
new AxisInfo("north", AxisOrientationEnum.North),
new AxisInfo("east", AxisOrientationEnum.East));
List<ProjectionParameter> parameters = new List<ProjectionParameter>
{
new ProjectionParameter("semi_major", 6378137.0),
new ProjectionParameter("semi_minor", 6378137.0),
new ProjectionParameter("latitude_of_origin", 0.0),
new ProjectionParameter("central_meridian", 0.0),
new ProjectionParameter("scale_factor", 1.0),
new ProjectionParameter("false_easting", 0.0),
new ProjectionParameter("false_northing", 0.0)
};
IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
IProjectedCoordinateSystem targetCs = csFac.CreateProjectedCoordinateSystem(
"Google Mercator",
sourceCs,
projection,
LinearUnit.Metre,
new AxisInfo("East", AxisOrientationEnum.East),
new AxisInfo("North", AxisOrientationEnum.North));
ICoordinateTransformation transformation = ctFac.CreateFromCoordinateSystems(sourceCs, targetCs);
return transformation;

Geotools - get beside parces

I have a problem needed help. I have parces map layer in postgis which contain polygons. This layer is following by not overlap topology rule.
How could i get features that "beside" a selected feature with geotools api? The "beside" feature is feature have at least one same edge.
For example in this picture, when select feature A, i need to get feature B,C,D, not get feature E.
Any help is high appreciate! Thanks!
I solve my problem by this code
public static void getRoundFeature(SimpleFeature feature, SimpleFeatureSource featureSource){
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
Geometry g = (Geometry) feature.getAttribute("the_geom");
Polygon polygon = geometryFactory.createPolygon(g.getCoordinates());
//Polygon poly = (Polygon) feature;
Filter filter = ff.intersects(ff.property("the_geom"), ff.literal(polygon));
try {
SimpleFeatureCollection featureCollection = featureSource.getFeatures(filter);
//System.out.println("feature around: "+ featureCollection.size());
FeatureIterator iter = featureCollection.features();
while(iter.hasNext()){
SimpleFeature rfeature = (SimpleFeature) iter.next();
System.out.println("so to: "+rfeature.getAttribute("soto")+ "so thua: "+rfeature.getAttribute("sothua"));
}
} catch (IOException e) { LOGGER.log(Level.FINER, e.getMessage(), e);
}
}

Mongodb geoWithin google maps bounds

I'm kind of new to mongodb.
I want to use mongodb's geospatial feature to query locations in a boundary obtained from google maps. It's like withinBox(swLng, swLat, neLng, neLat). But it fails to give me correct results when the map zooms out, as neLng/neLat is less than swLng/swLat. It seems that this is calculated using x > swLng and x < neLng and y > swLat and y < neLat, not taking map projection into account.
So can I achieve the query by withinBox, or I have to adjust coordinates, or I should use near?
As I commented above, when northeast and southwest points cross the international date line, the query will fail as top right's y is smaller than bottom left's y.
If you are using mongodb with php and doctrine, you need make two queries and merge result on the client side like below:
if ($x1 > $x2) {
$qb->field('coordinates')->withinBox(-180, $y1, $x2, $y2);
$result1 = $qb->getQuery()->execute();
$qb->field('coordinates')->withinBox($x1, $y1, 180, $y2);
$result2 = $qb->getQuery()->execute();
$result = $result1->toArray() + $result2->toArray();
} else {
$qb->field('coordinates')->withinBox($x1, $y1, $x2, $y2);
$result = $qb->getQuery()->execute();
$result = $result->toArray();
}