moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

Geo-location Positioning #91

Closed avazzan closed 10 years ago

avazzan commented 10 years ago

Hi. I try to use Google Maps geo-location coordinates for a project. When I add a marker for the start & give it geo numbers, the marker points out somewhere far from my desired point like 2 miles away! I suppose the problem is that I might not have used the precise coordinates for the corners of the map which I got by eye-comparing the picture & the actual Google map! Could it be that or is there something else? Thanks

moagrius commented 10 years ago

That might be it, I can't say for sure without seeing the app. FWIW, the relative positioning system just uses basic math. If you have a top-left coordinate of say 45.50000 and a bottom-right of 46.50000 and you pass in a coordinate for a marker at 46.00000, it's going to be exactly halfway between - there's no actual geo-location happening.

Here's some psuedo-code to illustrate the logic and math:

int distance = right - left;
int position = coordinate.x - left;
float relative = position / (float) distance;
int pixel = tileView.definedWidth * relative;

Note also that the relative positioning system isn't tightly coupled with latitude and longitude, and this throws people off sometimes (traditionally, most API define points as x, y, while geo location uses lat, lng, which is actually the opposite - latitude is the y axis, and longitude is the x axis). As I said, it's all relative so as long as you give the order of your space in the same order that you're querying it, it should be fine.

HTH

avazzan commented 10 years ago

So you mean i can give any coordinates i want regardless of Google maps'? But i wanna use an offline map so it loads first thing the app starts, then do online things like GPS & navigation so i have to give the exact coordinates of the online map to my app! You see in the picture I give the coordinates of point #1 but it goes to #2 at top right! googlemap Here are the codes : TileView tileView = getTileView(); tileView.setSize(8962, 17000); tileView.addDetailLevel(0.125f, "tiles/125%col%%row%.png", "map250.jpg"); tileView.addDetailLevel(1.000f, "tiles/1000%col%%row%.png", "map250.jpg"); tileView.addDetailLevel(0.500f, "tiles/500%col%%row%.png", "map250.jpg"); tileView.addDetailLevel(0.250f, "tiles/250%col%%row%.png", "map250.jpg"); tileView.setMarkerAnchorPoints(-0.5f, -1.0f); tileView.setScaleLimits(0, 2); tileView.setTransitionsEnabled(false); tileView.setScale(0.5f); tileView.defineRelativeBounds(35.291349, 58.441143, 35.216722, 58.489264);

    Paint paint = tileView.getPathPaint();
    paint.setShadowLayer(4, 2, 2, 0x66000000);
    paint.setPathEffect(new CornerPathEffect(5));

    double[] point = new double[]{35.232987, 58.462303};
    ImageView marker = new ImageView(this);
    marker.setTag(point);
    marker.setImageResource(R.drawable.maps_marker_blue);
    marker.setOnClickListener(markerClickListener);
    tileView.addMarker(marker, point[0], point[1], -0.5f, -1.0f);
    frameTo(point[0], point[1]);
moagrius commented 10 years ago

I've got one foot out the door, but try flipping your coordinate around... Like tileView.defineRelativeBounds(35.216722, 58.441143, 35.291349, 58.489264); - if that doesn't work just juggle the numbers. Put a path or marker at each corner to see what's appearing where, etc.

HTH - I'll check back later when I have more time

moagrius commented 10 years ago

you might also want to try flipping your lats and lngs - in either or both the defineRelativeBounds or the coordinates themselves. the best advice i can give is to just try either placing markers in the corners so you can see what's happening clearly, or just start flipping coordinates until things start to make sense.

liuxingxing commented 10 years ago

tileView.setDecoder(new BitmapDecoderHttp()); tileView.setSize(Integer.parseInt(hall.getMapWidth()), Integer.parseInt(hall.getMapHeight())); String imageUrl = Config.SERVER_ADDRESS + hall.getMapSrc(); tileView.addDetailLevel( 0.0f, "", imageUrl);

tileView.setScale(1); tileView.setScaleLimits(0,2);

please tell me why¡£ wo use your code like this,but I meet this error: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118) at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163) at libcore.io.IoBridge.recvfrom(IoBridge.java:513) at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) at java.io.InputStream.read(InputStream.java:163) at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142) at java.io.BufferedInputStream.read(BufferedInputStream.java:227) at libcore.io.Streams.readAsciiLine(Streams.java:201) at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560) at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813) at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274) at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) at com.qozix.tileview.graphics.BitmapDecoderHttp.decode(BitmapDecoderHttp.java:27) at com.qozix.tileview.samples.SampleManager.update(SampleManager.java:51) at com.qozix.tileview.TileView.resume(TileView.java:808) at com.autochina.modules.exhibition_guide.part.TileViewBasePart.onResume(TileViewBasePart.java:26) at com.autochina.modules.exhibition_guide.part.HallTileViewPart.onResume(HallTileViewPart.java:116)

moagrius commented 10 years ago

You can't do this:

tileView.addDetailLevel( 0.0f, "", imageUrl);

There's multiple problems here. If you're only using one detail level, the scale should be 1, not 0, so the first argument is incorrect, although the real effect of this is probably negligible.

The second argument is a path that's going to be passed to the BitmapDecoderHttp instance, which is going to try to download it from the web (which is where your actual error is from).

If you just want to use it for a stretchable background, you'd be better off approaching it differently, and probably not using TileView at all (although you could probably use some widgets in the package, like a ZoomPanLayout and a ScalingLayout).

FWIW, I'm not sure why you posted this here - it should probably be in it's own thread.

HTH

moagrius commented 10 years ago

@liuxingxing did you get this worked out? I'm going to close this issue, but if you're still having problems feel free to start a new thread.