Pergunta

I'm getting latitude and longitude as strings from a Google Places URL. Now I'd like to place a pin on a map using the obtained coordinates. Something is goofy because I'm trying to parse the strings into integers for the GeoPoint, and the results show as 0,0 so the pin is placed off the coast of Africa. Here's my code:

    int lati5Int, longi5Int;        

String latiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LAT);
    String longiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LNG);
    TextView getLatiStringtv.setText(latiString);
    TextView getLongiStringtv.setText(longiString);

    try { 
        lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
        longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());
    } catch(NumberFormatException nfe) { 
       System.out.println("Could not parse " + nfe); 
    }
// shows that the ints are zeros
    doubleLatiTV.setText(Integer.toString(lati5Int));
    doubleLongiTV.setText(Integer.toString(longi5Int));

//--- GeoPoint---
newPoint = new GeoPoint(lati5Int,longi5Int);
mapController.animateTo(newPoint);
mapController.setZoom(17);
//--- Place pin ----
marker = getResources().getDrawable(R.drawable.malls);      

OverlayItem overlaypoint = new OverlayItem(newPoint, "Boing", "Whattsup");
CustomPinpoint customPin = new CustomPinpoint(marker, SMIMap.this);
customPin.insertPinpoint(overlaypoint);
overlayList.add(customPin);

I think the error is in the parsing of the integers:

            lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
        longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());

I think the parsing sees the decimal point in the coordinates and freaks out. So how can I parse the coordinate strings into integers so that the GeoPoint will see them as correctly formatted coordinates like: 30.487263, -97.970799

Foi útil?

Solução

GeoPoint doesn't want to see them as 30.487263, -97.970799. It wants them as the integers 30487263, -97970799. So like A.A said, parse as double first, multiply by E6, then cast to int.

So maybe something like:

lati5Int = Double.parseDouble(getLatiStringtv.getText().toString());
latiE6 = (int) (lati5Int*1000000);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top