Categories
discuss

What does map.setMyLocationEnabled(true) really do

I am setting:

map.setMyLocationEnabled(true)

But I am wondering what this really does. What I know:

  1. I get a locate me button in the upper right corner of the map
  2. I get a blue icon on the map that represents my current location

Here is my concern. I am writing a location aware app but I am concerned about battery. Elsewhere in my app (via preferences) I set up a LocationManager and listen for location events so I can log them. But part of my preferences is the accuracy at which I get updates and the interval.

When I turn off my LocationManager:

locationManange.removeUpdates(LocationListener listener);

I expect to turn off location services for the entire app. Yet I still see the little GPS icon in my phones header bar indicating the app is getting location updates. I know that is coming from the fact that I set this on my map view:

map.setMyLocationEnabled(true);

I originally thought that if you did not listen for Location updates by setting up a LocationManager that set map.setMyLocationEnabled(true) would have no effect (ie no my location icon, or button). However that is not true.

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings? I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible? Did I miss something in the docs? I cannot find anything about the accuracy or interval that is setup on a LocationManager when I call map.setMyLocationEnabled(true).

Answer

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings?

Not exactly. Maps V2 uses LocationClient by default.

I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible?

You can use setLocationSource() to supply your own location data for use with the my-location layer. Here is a sample project demonstrating this.

Categories
discuss

libgdx draw chinese characters

I like to print Chinese text in my application.

1.When I try this, the screen will be empty. There is no error at the console.

Create method:

FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40, "好", false);

Render method:

spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "好", 10, 100);
spriteBatch.end();

2.When I try this, 3 different Chinese characters show up on screen but I have no idea why these characters where draw. There is no connection between asd and the three characters

Create method:

FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/DFLS1B.TTF"));
font = gen.generateFont(40);

Render method:

spriteBatch.setColor(1, 1, 1, 1);
spriteBatch.begin();
font.draw(spriteBatch, "asd", 10, 100);
spriteBatch.end();

Does anyone know how to draw Chinese character in libgdx correct (I use the current version of libgdx)? For example: How are you? – Ni hao ma? – 你 好 吗?

Greetings


EDIT: Here is a full example which will show expected Chinese characters on the screen. I have downloaded the font from here: http://www.study-area.org/apt/firefly-font/

package com.mytest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class ChineseFontTest implements ApplicationListener {

private Stage stage;
private SpriteBatch spriteBatch;
public BitmapFont font;

@Override
public void create() {
    stage = new Stage(800, 800);
    spriteBatch = new SpriteBatch();

    FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/fireflysung.ttf"));
    font = gen.generateFont(40, "好你吗", false);

}

@Override
public void dispose() {
    stage.dispose();
}

@Override
public void render() {      
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    spriteBatch.setColor(1, 1, 1, 1);
    spriteBatch.begin();
    font.draw(spriteBatch, "你好吗", 10, 100);
    spriteBatch.end();
}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
    }

Answer

Go to http://www.angelcode.com/products/bmfont/ and get BMFont and use it to look at your font. You can also pregenerate libgdx bitmap font files with it. The problem is that your font is not a unicode font.

When you are typing Java code the “好” symbol is translated to the value U+597D. The font ttf file you are using is an old-school non-unicode font. The way these older font files work is that they replace a letter like “a” with a different picture. “a” is U+61. So in your program the letter “a” is converted to U+61 which maps to a chinese character symbol but “好” with a value of U+597D does not.

You can either continue to use your font and look up the position of every character so that you can use the correct number. Don’t use “a” instead use something like (char)0x61 so it is a little less confusing. Or…

Just get a valid unicode font that contains the “好” symbol at U+597D.

Categories
discuss

How to split a byte array around a byte sequence in Java?

How to split a byte[] around a byte sequence in Java? Something like the byte[] version of String#split(regex).

Example

Let’s take this byte array:
[11 11 FF FF 22 22 22 FF FF 33 33 33 33]

and let’s choose the delimiter to be
[FF FF]

Then the split will result in these three parts:
[11 11]
[22 22 22]
[33 33 33 33]

EDIT:

Please note that you cannot convert the byte[] to String, then split it, then back because of encoding issues. When you do such conversion on byte arrays, the resulting byte[] will be different. Please refer to this: Conversion of byte[] into a String and then back to a byte[]

Answer

Note that you can reliably convert from byte[] to String and back, with a one-to-one mapping of chars to bytes, if you use the encoding “iso8859-1”.

However, it’s still an ugly solution.

I think you’ll need to roll your own.

I suggest solving it in two stages:

  1. Work out how to find the of indexes of each occurrence of the separator. Google for “Knuth-Morris-Pratt” for an efficient algorithm – although a more naive algorithm will be fine for short delimiters.
  2. Each time you find an index, use Arrays.copyOfRange() to get the piece you need and add it to your output list.

Here it is using a naive pattern finding algorithm. KMP would become worth it if the delimiters are long (because it saves backtracking, but doesn’t miss delimiters if they’re embedded in sequence that mismatches at the end).

public static boolean isMatch(byte[] pattern, byte[] input, int pos) {
    for(int i=0; i< pattern.length; i++) {
        if(pattern[i] != input[pos+i]) {
            return false;
        }
    }
    return true;
}

public static List<byte[]> split(byte[] pattern, byte[] input) {
    List<byte[]> l = new LinkedList<byte[]>();
    int blockStart = 0;
    for(int i=0; i<input.length; i++) {
       if(isMatch(pattern,input,i)) {
          l.add(Arrays.copyOfRange(input, blockStart, i));
          blockStart = i+pattern.length;
          i = blockStart;
       }
    }
    l.add(Arrays.copyOfRange(input, blockStart, input.length ));
    return l;
}
Categories
discuss

What’s the difference between instance method reference types in Java 8?

So Java 8 introduces method references and the docs describe the four types.

My question is what’s the difference between the two instance types?

  1. Reference to an instance method of a particular object.
  2. Reference to an instance method of an arbitrary object of a particular type.

Both refer to references but what’s significantly different? Is it that the type inference used to resolve them is different? Is it significant that (in their examples) one is a closure and the other is a lambda? Is it something to do with the number of arguments on a method?

Answer

  1. myString::charAt would take an int and return a char, and might be used for any lambda that works that way. It translates, essentially, to index -> myString.charAt(index).

  2. String::length would take a String and return an int. It translates, essentially, to string -> string.length().

  3. String::charAt would translate to (string, index) -> string.charAt(index).

Categories
discuss

javascript hover one element to change text color of another

I feel that I am getting close but am new to javascript. Is there anything wrong with this code. I want when you hover on element “research_arrow” to change the text color of “research_link”.

$(document).ready(function () {
  $(".research_arrow").hover(function () {
    $(".research_link").css("color:#ffffff");
  });
  $(".research_arrow").mouseleave(function () {
     $(".research_link").css("color:#000000");
  });
});

Answer

Try this,

$(document).ready(function(){
  $(".research_arrow").hover(function() {
     $(".research_link").css("color", "#ffffff");
  }, function() {
     $(".research_link").css("color", "#000000");
  });
});

hover accepts two function one like mouseover and other like mouse out.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..