Select to view content in your preferred language

Simple Map Point Question

1830
16
Jump to solution
10-21-2012 07:51 AM
PhilipTownsend
Emerging Contributor
I am new to the Flex API but not new to GIS programming.

I have a message service that will be reporting locations via Lat/Lon and I will need to locate a simple graphic on the map specified by the incoming location coordinates. It seems like this should be really simple. I have created a simple click event for testing. The points show up at the correct locations, but how do I get them to be represented by lat/lon coordinates? Obviously, the end goal is not to be using mouse clicks to determine locations. Thanks!

         <esri:SpatialReference id="wgs" wkid="4326"/>

   private function onMapClick(event:MapMouseEvent):void{
    var g:Graphic = new Graphic();
    //g.geometry = new MapPoint(38.0594, 81.1121, wgs); //event.mapPoint;
    g.geometry = event.mapPoint;
    grLayer.add(g);
    Alert.show("Mouse clicked at: " + event.mapPoint.x + "|" + event.mapPoint.y);
   }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
He just needed to project the point into Web Mercator, as asked in another thread.

View solution in original post

0 Kudos
16 Replies
AnthonyGiles
Honored Contributor
Philip,

Assign the lat/long coords to two separate variables or to an array then use these variables when creating your mappoint, ie if you have a string called coordstring set to "38.0594,81.1121"

var coords:Array = coordstring.split(",");
var g:Graphic = new Graphic(new MapPoint(new MapPoint(coords[0],coords[1],wgs)));
grLayer.add(g);

Regards

Anthony
0 Kudos
IvanBespalov
Frequent Contributor
Philip,
Sample 1 (with sources) - Project a point using GeometryService

Sample 2 (with sources) - Project a point using WebMercatorUtil

Good luck.
0 Kudos
PhilipTownsend
Emerging Contributor
Thanks for the reply Anthony!

Unfortunately something else is wrong. When I pass in the coordinates directly to the graphic variable,

var g:Graphic = new Graphic();
g.geometry = new MapPoint(38.0594, 81.1121, wgs);

the graphic appears but is placed at 0,0 rather than at the specified coordinates. Any idea why this is happening?

Thanks!
0 Kudos
AnthonyGiles
Honored Contributor
Philip,

What happens if you add the graphic this way:

var g:Graphic = new Graphic(new MapPoint(new MapPoint(38.0594, 81.1121,wgs)));

Regards

Anthony
0 Kudos
KenBuja
MVP Esteemed Contributor
He just needed to project the point into Web Mercator, as asked in another thread.
0 Kudos
TomRauch
Emerging Contributor
Hi, I am a bit stumped - I would like to use the myMap.zoom(1 / 16, myGraphicMarker) I saw Robert S. post in another thread.

But...

var myGraphicMarker:Graphic = new Graphic(new WebMercatorMapPoint(longitude, latitude),
      new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
    
     myGraphicsLayer.add(myGraphicMarker);
     myGraphicsLayer.refresh();

          myMap.zoom(1 / 16, myGraphicMarker);

when I run that, Flex tells me I have an error 1067: Implicit coercison of :Graphic to unrelated type: MapPoint (truncated error message).  I understand what a coercion problem is, but I am curious why MyGraphicMarker is not recognized as a MapPoint.  Is it because I am declaring a WebMercatorMapPoint?

Thanks, Tom
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tom,

   It is because myGraphicMarker is a graphic and not a MapPoint or a class that extends geometry.

Try
myMap.zoom(1 / 16, myGraphicMarker.geometry as MapPoint);
0 Kudos
TomRauch
Emerging Contributor
Thank you Robert, I suspected that was the case...

However, when I run your code above, I now receive:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

I moved the 'refresh' property below the zoom, thinking that might make a difference but, I still get the error.

  
          myMap.zoom(1/16, myGraphicMarker.geometry as MapPoint);
      
       myGraphicsLayer.refresh();

Thanks again, Tom
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Tom,

  Can you put a breakpoint in your code on this line an test to see what is null? Maybe you need to use:

myMap.zoom(1/16, myGraphicMarker.geometry as WebMercatorMapPoint);
0 Kudos