I have been trying to get Lat Lng from this code using the same call to Google Geocoding API, but I have not found a way.
Any suggestion would be greatly appreciated!

/*!
 * geotext v1.0
 *
 * https://github.com/Frizzled/geotext
 *
 * Copyright (c) 2014 Vladimir Loscutoff
 * Released under the MIT license
 */
var GeoText = (function ($, gMaps, undefined) {
    'use strict';

    function GeoText(vars) {
        this.vars = { // Settings
            'name' : 'default',
            'delimiter' : ', '
        };
        this.data = { // Settings
            'success' : false
        };

        // Merge settings
        if (vars !== undefined) { this.vars = $.extend(this.vars, vars); }

        this.init();
    }

    GeoText.prototype.init = function() {
        var that = this;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                function (location) {
                    var point = new gMaps.LatLng(location.coords.latitude, location.coords.longitude);
                    new gMaps.Geocoder().geocode({'latLng': point}, function (res, status) {
                        if(status === gMaps.GeocoderStatus.OK && res[0] !== undefined) {
                            that.setLocation(res[0]);
                        }
                    });
                }
            );
        }
    };

    GeoText.prototype.setLocation = function(location) {
        var that = this;
        that.data.success = true;
        $.each(location.address_components, function(k,v1) {
            $.each(v1.types, function(k2, v2) { 
                that.data[v2]=v1.short_name;
                that.data[v2+'_long']=v1.long_name;
            });
        });
        that.applyText();
    };

    GeoText.prototype.applyText = function() {
        var that = this;
        var geoFields = $('[class*=geotext]');
        $.each(geoFields, function(key, field) {
            var $field = $(field);
            var delimiter = $field.data('geotext-delimiter') || that.vars.delimiter;
            var text = that.parseField($field, delimiter);
            if (text) {
                // Check for leading or following text
                if ($field.data('geotext-text-before')) { text = $field.data('geotext-text-before') + text; }
                if ($field.data('geotext-text-after')) { text = text + $field.data('geotext-text-after'); }

                if ($field.is('input')) {
                    $field.val(text).change();
                } else {
                    $field.html(text);
                }
            }
        }); 
    };

    GeoText.prototype.parseField = function(field, delimiter) {
        var that = this;

        // Get rules
        var getRules = /geotext\[(.*)\]/.exec(field.attr('class'));
        if (!getRules) { return false; }
        var str = getRules[1];
        var rules = str.split(/\[|,|\]/);
        $.each (rules, function(key, rule) {
            rules[key] = rule.replace(" ", "");
            if (rules[key] === '') { delete rules[key]; }
        });

        // Generate text
        var text = '';
        $.each (rules, function(key, rule) {
            try {
                switch (rule) {
                    case "address": text += that.data.street_number +' '+ that.data.route; break;
                    case "street": text += that.data.route; break;
                    case "street-long": text += that.data.route_long; break;
                    case "city": text += that.data.locality; break;
                    case "city-state": text += that.data.locality +delimiter+ that.data.administrative_area_level_1; break;
                    case "city-state-zip": text += that.data.locality +delimiter+ that.data.administrative_area_level_1 + ' ' + that.data.postal_code; break;
                    case "state": text += that.data.administrative_area_level_1; break;
                    case "state-long": text += that.data.administrative_area_level_1_long; break;
                    case "zip": text += that.data.postal_code; break;
                    case "county": text += that.data.administrative_area_level_2; break;
                    case "country": text += that.data.country; break;
                    case "country-long": text += that.data.country_long; break;
                    case "lat":  location.coords.latitude; break;
                }
                if (rules[(key+1)] !== undefined) { text += delimiter; }
            } catch (ignore) {}
        });

        return text;
    };

    return GeoText;
})(jQuery, google.maps);

Recommended Answers

I can't decode what your code is doing. You wrote you want the Lat+Long yet on line 33 you tell it the lat and long.

To help me understand what your code is supposed to do, supply a few lines of psuedo code.

Jump to Post

You are allowed to edit your post within 30 minutes of iniitially posting it.

Jump to Post

Lat and Long do not tell you the address but a point on a map. I've found folk new to this complain that the Lat+Long to address is "always wrong." They need time to think about how this works as they are not inside the address's boundery but usually on …

Jump to Post

For future reference, Code Snippet is meant for posting working and documented snippets of code. It is not to be used when you are posting a piece of code that you are having problems with. Please post under Discussion/Question.

Jump to Post

PS. My project is to drive around, geocode a location, add my observations, and then have a map with my addresses and notes.

As a realtor what good is Lat+Long doing us here? All the realtor/realty sites use/display addresses. Maybe the Lat+Long is complicating the project.

I rarely see folk …

Jump to Post

All 10 Replies

I can't decode what your code is doing. You wrote you want the Lat+Long yet on line 33 you tell it the lat and long.

To help me understand what your code is supposed to do, supply a few lines of psuedo code.

Thank you. This is the original geotext code. To parse the address, code comes with classes that parses the addresses:
case "country": text += that.data.country; break;
I n need case lat, case lng so I can usem them in a map.
See this fiddle.
Click Here
Lat and lng are not under address_component.
It is under geometry. I am not a programmer, and I have tried this for hours, now days.
I do not want to add a new piece of code to call the api again due to costs. The same geocoding, as you mentioned, brings the lat lng, I just need to able to parse it.
Line 107 (on this site) makes no good (see original link above for working code). I was trying to parse it and forgot to remove that line. It looks like we can't edit the post.
Thank you!!
PS. My project is to drive around, geocode a location, add my observations, and then have a map with my addresses and notes.

This new version of Daniweb is difficult to edit what you wrote. you can't even delete your post and re-write it. No good.

You are allowed to edit your post within 30 minutes of iniitially posting it.

commented: I don't like it... I was planning to delete and post the same thing again... but I can't. +2

Lat and Long do not tell you the address but a point on a map. I've found folk new to this complain that the Lat+Long to address is "always wrong." They need time to think about how this works as they are not inside the address's boundery but usually on the street or sidewalk. Only the human that is doing this can make this decision.

But back to your top post and reply. To me you tossed out a lot of code and maybe, just maybe told what you wanted in your one line PS in the reply. All that code and it's just me saying this, distracts from what you want to do.

-> Don't take this badly, but leaving out comments about what a passage does means that when you wrote it, you and God knows what the intent was. Yes I can read code but I can't know the intent and God doesn't speak often on the matter.

My advice is to break down the problem to steps.

  1. As I travel I want my phone to log a location with my notes.
  2. Later I want to translate these locations to an address.
  3. Then I want a map with ????

Each of those goals should be tackled on their own and broken down into discrete small steps that you can code up. As to you not being a programmer, now you are.

commented: http://aaa.properties/warriors/createTest.php And I need to populate lat and lng. +2

For future reference, Code Snippet is meant for posting working and documented snippets of code. It is not to be used when you are posting a piece of code that you are having problems with. Please post under Discussion/Question.

Gloak commented: The current task is to store Lat Lng in the database.

That doesn't appear to be what the code at the top does at all. I'd start a new app for my phone that:

  1. Shows me on a map. This alone should take you some time.
  2. Shows a button with maybe "LOG MY CURRENT LOCATION".
  3. That button's code would get the current Lat+Long and write it to a file or database.
  4. Now work out how to get user comments into that record or file for this Lat+Long.

Break down the problem. Don't try to swallow it whole.

The top post omitted so much that it's best forgotten. No tags about the host computer but this looks like a prime use for a smart phone.

Daniweb has change quite a bit. I can't see my comments to @rproffitt. It blinks on and off many times per second, some bug.
So this is my page I need to populate with lat lng.
I was trying to create class="geotext[lat]", class="geotext[lng]" so I can have/autocomplete the lat and lng in the form. Then I will save it to DB. That is all. I have a file that brings the info to a map with all the details I am saving.
(This code is not mine, as you can see in the long initial post, I left the comments at the beginning with the info to github. I am realtor, doing most reverse engineering, getting code in the net, adjusting it to my needs.).

PS. My project is to drive around, geocode a location, add my observations, and then have a map with my addresses and notes.

As a realtor what good is Lat+Long doing us here? All the realtor/realty sites use/display addresses. Maybe the Lat+Long is complicating the project.

I rarely see folk here code for the members. Only discuss, try to sort out an error and redesign. That said, it sounds almost like you want to duplicate Zillow or Redfin which should be a long endeavor as your programming skills improve.

My PS. Don't get stuck on just Google APIs. Use any solution. Such as https://www.google.com/search?q=street+address+to+lat+long&gl=US

commented: Thank you. +0

Thank you. It's solved in the original code.
It takes me 2 days to solve something that is literally 4 lines of code. Sometimes, people help me in these forums, so I ask after spending and good amount of time.